Geometer.h

Go to the documentation of this file.
00001 /*
00002  *
00003  *  Acoustic Geometer Class
00004  *  Header
00005  *
00006  *  Brent Lehman
00007  *  17 April 2002
00008  *
00009  *
00010  */
00011 
00012 #ifndef GEOMETER_H
00013 #define GEOMETER_H
00014 
00015 #include "CyberX3D.h"
00016 
00017 // The author wanted to eliminate unnecessary function calls 
00018 // so he developed these macros for vector arithmetic.  For 
00019 // those who don't know, "##" is a token-pasting operator.  
00020 // It directs the preprocessor to paste together the tokens 
00021 // found on either side of the operator.  For example, if the 
00022 // variable "vec" is passed to the EXPAND macro below, as in 
00023 // "EXPAND(vec)", the result of the macro will be "vec_x, 
00024 // vec_y, vec_z".  The author had never used this operator 
00025 // before.  The C++ compiler in Microsoft Visual Studio 6.0 
00026 // handles it well and the convention appears to be portable.  
00027 // The author hasn't actually tried it with other compilers.
00028 
00029 // To use these macros the programmer declares three floating 
00030 // point variables per vector.  Each vector represents a 
00031 // different Cartesian component of that vector and as such 
00032 // has "_x", "_y", or "_z" as a suffix.  Each triplet has the 
00033 // same prefix.  The programmer passes on the prefixes to the 
00034 // macros, which rely on the preprocessor to append the 
00035 // appropriate suffixes.  Developers generally use structs 
00036 // with members named "x", "y", and "z", but the author 
00037 // didn't trust the compiler to handle the memory access for 
00038 // these constructs efficiently.
00039 
00040 using namespace CyberX3D;
00041 
00042 namespace csl {
00043 
00044 #define EXPAND(a) a##_x, a##_y, a##_z
00045 #define ZERO(a) ((a##_x == 0.0f) && (a##_y == 0.0f) && (a##_z == 0.0f))
00046 #define SET(a, x, y, z) \
00047     a##_x = x; \
00048     a##_y = y; \
00049     a##_z = z;
00050 #define VECTOR_COPY(d, s) \
00051     d##_x = s##_x;\
00052     d##_y = s##_y;\
00053     d##_z = s##_z;
00054 #define SCALAR_PRODUCT(p, s, a) \
00055     p##_x = s * a##_x;\
00056     p##_y = s * a##_y;\
00057     p##_z = s * a##_z;
00058 #define DOT_PRODUCT(a, b) \
00059     (a##_x*b##_x + a##_y*b##_y + a##_z*b##_z)
00060 #define CROSS_PRODUCT(p, a, b) \
00061     p##_x = a##_y * b##_z - a##_z * b##_y; \
00062     p##_y = a##_z * b##_x - a##_x * b##_z; \
00063     p##_z = a##_x * b##_y - a##_y * b##_x;
00064 #define COMPONENTWISE_PRODUCT(p, a, b) \
00065     p##_x = a##_x * b##_x;\
00066     p##_y = a##_y * b##_y;\
00067     p##_z = a##_z * b##_z;
00068 #define VECTOR_SCALE(p, s) \
00069     p##_x *= s;\
00070     p##_y *= s;\
00071     p##_z *= s;
00072 #define VECTOR_SUM(s, a, b) \
00073     s##_x = a##_x + b##_x;\
00074     s##_y = a##_y + b##_y;\
00075     s##_z = a##_z + b##_z;
00076 #define VECTOR_ADD(s, a) \
00077     s##_x += a##_x;\
00078     s##_y += a##_y;\
00079     s##_z += a##_z;
00080 #define VECTOR_DIFF(s, a, b) \
00081     s##_x = a##_x - b##_x;\
00082     s##_y = a##_y - b##_y;\
00083     s##_z = a##_z - b##_z;
00084 #define MAGNITUDE(a) \
00085     sqrt(a##_x*a##_x + a##_y*a##_y + a##_z*a##_z)
00086 #define PROJECT(p, t, a, b, n, s) \
00087     VECTOR_DIFF(p, a, b) \
00088     if (ZERO(p)) \
00089     { \
00090         if (s == DOT_PRODUCT(n, a)) \
00091         { \
00092             t = 0.5f; \
00093             VECTOR_COPY(p, a) \
00094         } \
00095         else \
00096         { \
00097             t = 1e38f; \
00098             p##_x = 1e38f; \
00099             p##_y = 1e38f; \
00100             p##_z = 1e38f; \
00101         } \
00102     } \
00103     else \
00104     { \
00105         t = (s - DOT_PRODUCT(n, b)) / DOT_PRODUCT(n, p); \
00106         VECTOR_SCALE(p, t) \
00107         VECTOR_ADD(p, b) \
00108     }
00109 #define REFLECT(r, t, a, n, s) \
00110     t = s - DOT_PRODUCT(n, a); \
00111     t += t; \
00112     SCALAR_PRODUCT(r, t, n) \
00113     VECTOR_ADD(r, a)
00114 #define XYROTATE(r, a, phi) \
00115     r##_x = a##_x*cos(phi) - a##_y*sin(phi); \
00116     r##_y = a##_x*sin(phi) + a##_y*cos(phi); \
00117     r##_z = a##_z;
00118 
00119 
00120 typedef struct {
00121 
00122     double distance;
00123     double hangle;
00124     double vangle;
00125 
00126 } soundray;
00127 
00128 
00129 typedef struct {
00130 
00131     int num_vertices; // How many vertices the polygon has
00132     int allocated_vertices; // How many vertices the data structure has room to record
00133     int* vertex_indices; // Indices into the global list of vertices
00134     float normal_x; // components of the polygons normal vector
00135     float normal_y;
00136     float normal_z;
00137     float offset; // ploygon's distance from the origin
00138     float* bounding_normals_x; // normals of polygon's bounding planes
00139     float* bounding_normals_y;
00140     float* bounding_normals_z;
00141     float* bounding_offsets; // bounding planes' distances from the origin
00142 
00143     void dump(float*, float*, float*);
00144 
00145 } polygon;
00146 
00147 
00148 class Geometer
00149 {
00150 
00151     soundray* current_rayset; // Rays resulting from most recent computing effort
00152     int num_rays; // Number of rays in the current rayset
00153     int rayset_spaces; // Number of rays this data structure has room to record
00154     float max_ray_length; // radius of the clipping sphere
00155     float source_x;
00156     float source_y;
00157     float source_z;
00158     float ear_x;
00159     float ear_y;
00160     float ear_z;
00161     float ear_hangle; // ear's counterclockwise angle of rotation; 0 is positive X asis
00162     float* vertices_x;
00163     float* vertices_y;
00164     float* vertices_z;
00165     int* vertices_num_polygons; // number of polygons that include each vertex
00166     int** vertices_polygon_indices; // indices of polygons that include each vertex
00167     int num_vertices; // total number of vertices in the world
00168     int allocated_vertices; // number of vertices this data structure has room to record
00169     polygon* polygons; // array of all of the polygons in the world
00170     int num_polygons; // total number of polygons in the world
00171     int allocated_polygons; // how much room
00172 
00173     void traverse(SceneGraph& sg); // traverse the scene graph
00174     void traverse(Node* n, SceneGraph& sg); // traverse a node of the scene graph
00175     // apply transform to every descendant of the given transform node
00176     void apply_transform(int starting_point, CyberX3D::TransformNode* transform_node);
00177 
00178     // These functions are described in the code
00179     bool _check_ray(float from_x, float from_y, float from_z,
00180         float to_x, float to_y, float to_z,
00181         polygon* exempta, polygon* exemptb);
00182     int _find_rays(int depth, float vsrc_x, float vsrc_y, float vsrc_z, 
00183         float last_distance=0.0);
00184 
00185 public:
00186 
00187     bool fileIsValid;
00188 
00189     Geometer();
00190     ~Geometer();
00191 
00192     void set_range(float r); // set max ray length, also known as radius of clipping sphere
00193     void set_source(float x, float y, float z);
00194     void set_ear(float x, float y, float z, float direction);
00195     void set_polygons(polygon* w, int n);
00196     void set_everything(char* filename);
00197     void set_everything_using_VRML(char* filename);
00198 
00199     soundray* find_rays();
00200     int number_of_rays() { return num_rays; }
00201 };
00202 
00203 }
00204 
00205 #endif

Generated on Fri Apr 6 20:18:13 2007 for CSL by  doxygen 1.4.5-20051010