src/training/common/intfeaturemap.cpp
int IntFeatureMap::ComputeOffsetFeature(int index_feature, int dir) const {
  INT_FEATURE_STRUCT f = InverseIndexFeature(index_feature);
  ASSERT_HOST(IndexFeature(f) == index_feature);
  if (dir == 0) {
    return index_feature;
  } else if (dir == 1 || dir == -1) {
    FCOORD feature_dir = FeatureDirection(f.Theta);
    FCOORD rotation90(0.0f, 1.0f);
    feature_dir.rotate(rotation90);
    // Find the nearest existing feature.
    for (int m = 1; m < kMaxOffsetDist; ++m) {
      double x_pos = f.X + feature_dir.x() * (m * dir);
      double y_pos = f.Y + feature_dir.y() * (m * dir);
      int x = IntCastRounded(x_pos);
      int y = IntCastRounded(y_pos);
      if (x >= 0 && x <= UINT8_MAX && y >= 0 && y <= UINT8_MAX) {
        INT_FEATURE_STRUCT offset_f;
        offset_f.X = x;
        offset_f.Y = y;
        offset_f.Theta = f.Theta;
        int offset_index = IndexFeature(offset_f);
        if (offset_index != index_feature && offset_index >= 0) {
          return offset_index; // Found one.
        }
      } else {
        return -1; // Hit the edge of feature space.
      }
    }
  } else if (dir == 2 || dir == -2) {
    // Find the nearest existing index_feature.
    for (int m = 1; m < kMaxOffsetDist; ++m) {
      int theta = f.Theta + m * dir / 2;
      INT_FEATURE_STRUCT offset_f;
      offset_f.X = f.X;
      offset_f.Y = f.Y;
      offset_f.Theta = Modulo(theta, 256);
      int offset_index = IndexFeature(offset_f);
      if (offset_index != index_feature && offset_index >= 0) {
        return offset_index; // Found one.
      }
    }
  }
  return -1; // Nothing within the max distance.
}
// Helper to compute an offset index feature. In this context an offset
// feature with a dir of +/-1 is a feature of a similar direction,
// but shifted perpendicular to the direction of the feature. An offset
// feature with a dir of +/-2 is feature at the same position, but rotated
// by +/- one [compact] quantum. Returns the index of the generated offset
// feature, or -1 if it doesn't exist. Dir should be in
// [-kNumOffsetMaps, kNumOffsetMaps] to indicate the relative direction.
// A dir of 0 is an identity transformation.
// Both input and output are from the index(sparse) feature space, not
// the mapped/compact feature space, but the offset feature is the minimum
// distance moved from the input to guarantee that it maps to the next
// available quantum in the mapped/compact space.
Helper to compute an offset index feature. In this context an offset
feature with a dir of +/-1 is a feature of a similar direction,
but shifted perpendicular to the direction of the feature. An offset
feature with a dir of +/-2 is feature at the same position, but rotated
by +/- one [compact] quantum. Returns the index of the generated offset
feature, or -1 if it doesn't exist. Dir should be in
[-kNumOffsetMaps, kNumOffsetMaps] to indicate the relative direction.
A dir of 0 is an identity transformation.
Both input and output are from the index(sparse) feature space, not
the mapped/compact feature space, but the offset feature is the minimum
distance moved from the input to guarantee that it maps to the next
available quantum in the mapped/compact space.
Helper to compute an offset index feature . In this context an offset feature with a dir of + / - 1 is a feature of a similar direction but shifted perpendicular to the direction of the feature . An offset feature with a dir of + / - 2 is feature at the same position but rotated by + / - one [ compact ] quantum . Returns the index of the generated offset feature or - 1 if it doesn ' t exist . Dir should be in [ - kNumOffsetMaps kNumOffsetMaps ] to indicate the relative direction . A dir of 0 is an identity transformation . Both input and output are from the index ( sparse ) feature space not the mapped / compact feature space but the offset feature is the minimum distance moved from the input to guarantee that it maps to the next available quantum in the mapped / compact space .
int IntFeatureMap::ComputeOffsetFeature(int index_feature, int dir) const {
  INT_FEATURE_STRUCT f = InverseIndexFeature(index_feature);
  ASSERT_HOST(IndexFeature(f) == index_feature);
  if (dir == 0) {
    return index_feature;
  } else if (dir == 1 || dir == -1) {
    FCOORD feature_dir = FeatureDirection(f.Theta);
    FCOORD rotation90(0.0f, 1.0f);
    feature_dir.rotate(rotation90);
    for (int m = 1; m < kMaxOffsetDist; ++m) {
      double x_pos = f.X + feature_dir.x() * (m * dir);
      double y_pos = f.Y + feature_dir.y() * (m * dir);
      int x = IntCastRounded(x_pos);
      int y = IntCastRounded(y_pos);
      if (x >= 0 && x <= UINT8_MAX && y >= 0 && y <= UINT8_MAX) {
        INT_FEATURE_STRUCT offset_f;
        offset_f.X = x;
        offset_f.Y = y;
        offset_f.Theta = f.Theta;
        int offset_index = IndexFeature(offset_f);
        if (offset_index != index_feature && offset_index >= 0) {
          return offset_index; 
        }
      } else {
        return -1; 
      }
    }
  } else if (dir == 2 || dir == -2) {
    for (int m = 1; m < kMaxOffsetDist; ++m) {
      int theta = f.Theta + m * dir / 2;
      INT_FEATURE_STRUCT offset_f;
      offset_f.X = f.X;
      offset_f.Y = f.Y;
      offset_f.Theta = Modulo(theta, 256);
      int offset_index = IndexFeature(offset_f);
      if (offset_index != index_feature && offset_index >= 0) {
        return offset_index; 
      }
    }
  }
  return -1; 
}
int IntFeatureMap :: ComputeOffsetFeature ( int index_feature , int dir ) const { INT_FEATURE_STRUCT f = InverseIndexFeature ( index_feature ) ; ASSERT_HOST ( IndexFeature ( f ) == index_feature ) ; if ( dir == 0 ) { return index_feature ; } else if ( dir == 1 || dir == -1 ) { FCOORD feature_dir = FeatureDirection ( f . Theta ) ; FCOORD rotation90 ( 0.0f , 1.0f ) ; feature_dir . rotate ( rotation90 ) ; for ( int m = 1 ; m < kMaxOffsetDist ; ++ m ) { double x_pos = f . X + feature_dir . x ( ) * ( m * dir ) ; double y_pos = f . Y + feature_dir . y ( ) * ( m * dir ) ; int x = IntCastRounded ( x_pos ) ; int y = IntCastRounded ( y_pos ) ; if ( x >= 0 && x <= UINT8_MAX && y >= 0 && y <= UINT8_MAX ) { INT_FEATURE_STRUCT offset_f ; offset_f . X = x ; offset_f . Y = y ; offset_f . Theta = f . Theta ; int offset_index = IndexFeature ( offset_f ) ; if ( offset_index != index_feature && offset_index >= 0 ) { return offset_index ; } } else { return -1 ; } } } else if ( dir == 2 || dir == -2 ) { for ( int m = 1 ; m < kMaxOffsetDist ; ++ m ) { int theta = f . Theta + m * dir / 2 ; INT_FEATURE_STRUCT offset_f ; offset_f . X = f . X ; offset_f . Y = f . Y ; offset_f . Theta = Modulo ( theta , 256 ) ; int offset_index = IndexFeature ( offset_f ) ; if ( offset_index != index_feature && offset_index >= 0 ) { return offset_index ; } } } return -1 ; }
Helper to compute an offset index feature.
Helper to compute an offset index feature .
// Find the nearest existing feature. // Found one. // Hit the edge of feature space. // Find the nearest existing index_feature. // Found one. // Nothing within the max distance.