00001 #include <esg/geometry/Point3D.h>
00002
00003 using namespace esg;
00004
00005 void Point3D::_duplicate_attributes(const Geometry& a)
00006 {
00007 _position.set(((Point3D&)a)._position);
00008 }
00009
00010 void Point3D::_rotateX(float a)
00011 {
00012 Matrix3 trMat;
00013 trMat.rotX(a);
00014 trMat.transform(_position);
00015 }
00016
00017 void Point3D::_rotateY(float a)
00018 {
00019 Matrix3 trMat;
00020 trMat.rotY(a);
00021 trMat.transform(_position);
00022 }
00023
00024 void Point3D::_rotateZ(float a)
00025 {
00026 Matrix3 trMat;
00027 trMat.rotZ(a);
00028 trMat.transform(_position);
00029 }
00030
00031 void Point3D::_rotate(float a, const Vector3& axis)
00032 {
00033 Matrix4 trMat;
00034 trMat.rotationGL(a, axis);
00035 trMat.transform(_position);
00036 }
00037
00038 void Point3D::_rotate(const Matrix3& rotMat)
00039 {
00040 rotMat.transform(_position);
00041 }
00042
00043 void Point3D::_translate(float x, float y, float z)
00044 {
00045 _position.add(Vector3(x, y, z));
00046 }
00047
00048 void Point3D::_transform(const Matrix4& trMat)
00049 {
00050 trMat.transform(_position);
00051 }
00052
00053 void Point3D::_scale(float s)
00054 {
00055 _position.scale(s);
00056 }
00057
00058
00059
00060
00061 Point3D::Point3D()
00062 {
00063 _position.set(0,0,0);
00064 }
00065
00066 Point3D::Point3D(const Vector3& v)
00067 {
00068 _position.set(v);
00069 }
00070
00071 Point3D::Point3D(float x, float y, float z)
00072 {
00073 _position.set(x,y,z);
00074 }
00075
00076 Point3D::Point3D(double x, double y, double z)
00077 {
00078 _position.set(x,y,z);
00079 }
00080
00081 void Point3D::rayIntersection(PointEnv* pPE,
00082 int mask,
00083 const Vector3& origin,
00084 const Vector3& direction,
00085 float maxDist)
00086 {
00087 pPE->mask = ENV_HAVE_NOTHING;
00088
00089 Vector3 dist(_position - origin);
00090 float cosinus = dist.dot(direction);
00091
00092 if (maxDist < MAXFLOAT && dist.length() > maxDist) return;
00093
00094 if (cosinus > (1 - EPS) && cosinus < (1 + EPS)) {
00095 pPE->mask |= ENV_HAVE_INTERFERENCE;
00096 if (mask & ENV_WANT_INTERSECTION) {
00097 pPE->intersection.set(_position);
00098 pPE->mask |= ENV_HAVE_INTERSECTION;
00099 }
00100 if (mask & ENV_WANT_DISTANCE) {
00101 pPE->distance = dist.length();
00102 pPE->mask |= ENV_HAVE_DISTANCE;
00103 }
00104 if (mask & ENV_WANT_UV_COORD) {
00105 pPE->uvCoord.set(0,0);
00106 pPE->mask |= ENV_HAVE_UV_COORD;
00107 }
00108 }
00109 }
00110
00111 bool Point3D::mapToUV(const Vector3& v, Vector2& uv)
00112 {
00113 return false;
00114 }
00115
00116 void Point3D::randomSample(int mask, PointEnv& pe, double* pdf)
00117 {
00118 pe.mask = ENV_HAVE_NOTHING;
00119
00120 if (mask & ENV_WANT_SURFACE_POINT) {
00121 pe.intersection.set(_position);
00122 pe.mask |= ENV_HAVE_SURFACE_POINT;
00123 }
00124 if (mask & ENV_WANT_UV_COORD) {
00125 pe.uvCoord.set(0,0);
00126 pe.mask |= ENV_HAVE_UV_COORD;
00127 }
00128
00129 if (pdf)
00130 cerr << "Sphere::randomDirection(): PDF is not implemented" << endl;
00131 }
00132
00133 bool Point3D::randomDirection(const Vector3& pov, Vector3& dir, double* pdf)
00134 {
00135 dir.set(_position);
00136 dir.sub(pov);
00137 dir.normalize();
00138
00139 if (pdf)
00140 cerr << "Sphere::randomDirection(): PDF is not implemented" << endl;
00141
00142 return true;
00143 }
00144
00145 Interval Point3D::extent(const Vector3& direction) const
00146 {
00147 Interval retInt;
00148 retInt.min = direction.dot(_position);
00149 retInt.max = retInt.min;
00150 return retInt;
00151 }
00152
00153 Geometry* Point3D::clone(const Matrix4* pTrMat) const
00154 {
00155 Point3D* pRet = new Point3D();
00156 pRet->_duplicate_attributes(*this);
00157 if (pTrMat) pRet->_transform(*pTrMat);
00158 return pRet;
00159 }
00160
00161 Vertex3 Point3D::centroid() const
00162 {
00163 return _position;
00164 }
00165
00166 double Point3D::radius() const
00167 {
00168 return 0.0;
00169 }
00170
00171 double Point3D::radius(const Vector3& centroid) const
00172 {
00173 return ((Vector3)(centroid-_position)).length();
00174 }
00175
00176 bool Point3D::separation(Geometry& geom, Vector3* pDir)
00177 {
00178 Vector3d v(geom.centroid() - _position);
00179 v.normalize();
00180 Interval i = geom.extent(v);
00181 float d = _position.dot(v);
00182 if (d < i.min || d > i.max) {
00183 if (pDir) pDir->set(v);
00184 return true;
00185 } else
00186 return false;
00187 }
00188
00189 double Point3D::distance(const Geometry& geom, Vector3* pDir)
00190 {
00191 Vector3d v(geom.centroid() - _position);
00192 v.normalize();
00193 Interval i = geom.extent(v);
00194 float d = _position.dot(v);
00195 float d1 = fabs(d-i.min);
00196 float d2 = fabs(d-i.max);
00197 if (d1 < d2) {
00198 if (pDir) pDir->set(v);
00199 return d1;
00200 } else
00201 return d2;
00202 }
00203
00204 void Point3D::dump(const char* intent, const char* tab)
00205 {
00206 printf("%s geometry Point {", intent);
00207 printf("%s %s position %f %f %f\n", intent, tab, _position.x, _position.y, _position.z);
00208 printf("%s }", intent);
00209 }