next up previous contents index
Next: Manipulating a Scene within Up: Affects Previous: Labels

Example Affect: Retrieving Positions and Vectors from Labels

This affect does not change the picture but shows how to retrieve the worldspace values of points, vectors and normals from the transform and the primitivespace points, vectors and normals.

  the_object = bind(the_object);
  the_object->label("object-label");
  vector pointp = {7, 0, 0};
  vector normal_at_q = {0, sqrt(0.5), sqrt(0.5)};
  the_object->affect = new PositionAffect("object-label",pointp, normal_at_q);

  the_object = shift(-2.5, 2, 0, stretch(1.5, 1.5, 1, 
                      rotate(Y ,Z, 90, the_object)));
  LINK(the_scene, PLUS, the_object);
  .
  .
  .
  
// The Affect is used for retrieving the position in 
// worldspace of vectors, points and normals
class PositionAffect : public Affect {
public:

  vector p_primitive;// point p in primitive space. also vector p - o
                     // as o is at the origin in worldspace
  vector n_primitive;/ /normal at q in primitive space.
  char * object_name;

  PositionAffect(char * obj_name, vector point_p, vector normal) {
    Vector::set(p_original, point_p);
    Vector::set(n_original,normal);
    object_name = obj_name;
    mark_for_deletion();
  }

  // Apply the affect procedure.
  virtual void affect_object(object the_obj){
    transform obj_trans;
    object found_object;
    int findresult = world_label_list->find(object_name, 
                                            &found_object, obj_trans);

    if (find_result == FALSE) {
      char tempbuf[MIRAGE_STRING_LENGTH];
      sprintf(tempbuf, "can't find the label %s", obj_name);
      fatal_errof("PositionAffect::affect", tempbuf,
                  "You have to give an object a label if you want a\n"
                  "to find the position of the object.\n );
      }
 
    vector object_position;
    vector p_position;
    vector p_vector;
    vector normal;
    Matrix::apply(obj_trans[FORE], zero_vector, object_position);
    Matrix::apply(obj_trans[FORE], p_original, p_position);
    Matrix::applynoshift(obj_trans[FORE], p_original, p_vector);
    Matrix::applytranspose(obj_trans[AFT], n_original, normal);

    printf("Origin shift: %f, %f, %f \n", object_position[X], 
            object_position[Y], object_position[Z]);
    printf("Point p: %f, %f, %f \n", p_position[X], 
           p_position[Y], p_position[Z]);
    printf("Vector v: %f, %f, %f \n", p_vector[X], p_vector[Y], p_vector[Z]);
    printf("Normal n: %f, %f, %f \n", normal[X], normal[Y], normal[Z]);
  }

  virtual ~PositionAffect() {
  }
  
};

Fig 19 shows the change to two points o and p and the vector (p - o) as well as the normal n as the arrow is transformed. The values of the changes are retrieved with the PositionAffect.

  figure480
Figure 19: Changes to Point and Vectors



Sophie Day
Fri Feb 20 15:47:19 NZDT 1998