00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 namespace Wm4
00018 {
00019
00020 template <class Real>
00021 Query3TRational<Real>::Query3TRational (int iVQuantity,
00022 const Vector3<Real>* akVertex)
00023 :
00024 Query3<Real>(iVQuantity,akVertex)
00025 {
00026 m_akRVertex = WM4_NEW RVector[m_iVQuantity];
00027 m_abEvaluated = WM4_NEW bool[m_iVQuantity];
00028 memset(m_abEvaluated,0,m_iVQuantity*sizeof(bool));
00029 }
00030
00031 template <class Real>
00032 Query3TRational<Real>::~Query3TRational ()
00033 {
00034 WM4_DELETE[] m_akRVertex;
00035 WM4_DELETE[] m_abEvaluated;
00036 }
00037
00038 template <class Real>
00039 Query::Type Query3TRational<Real>::GetType () const
00040 {
00041 return Query::QT_RATIONAL;
00042 }
00043
00044 template <class Real>
00045 int Query3TRational<Real>::ToPlane (int i, int iV0, int iV1, int iV2) const
00046 {
00047 int aiIndex[4] = { i, iV0, iV1, iV2 };
00048 Convert(4,aiIndex);
00049 return ToPlane(m_akRVertex[i],iV0,iV1,iV2);
00050 }
00051
00052 template <class Real>
00053 int Query3TRational<Real>::ToPlane (const Vector3<Real>& rkP, int iV0,
00054 int iV1, int iV2) const
00055 {
00056 RVector kRP;
00057 kRP[0] = Rational(rkP[0]);
00058 kRP[1] = Rational(rkP[1]);
00059 kRP[2] = Rational(rkP[2]);
00060 int aiIndex[3] = { iV0, iV1, iV2 };
00061 Convert(3,aiIndex);
00062 return ToPlane(kRP,iV0,iV1,iV2);
00063 }
00064
00065 template <class Real>
00066 int Query3TRational<Real>::ToTetrahedron (int i, int iV0, int iV1, int iV2,
00067 int iV3) const
00068 {
00069 int aiIndex[5] = { i, iV0, iV1, iV2, iV3 };
00070 Convert(5,aiIndex);
00071 return ToTetrahedron(m_akRVertex[i],iV0,iV1,iV2,iV3);
00072 }
00073
00074 template <class Real>
00075 int Query3TRational<Real>::ToTetrahedron (const Vector3<Real>& rkP, int iV0,
00076 int iV1, int iV2, int iV3) const
00077 {
00078 RVector kRP;
00079 kRP[0] = Rational(rkP[0]);
00080 kRP[1] = Rational(rkP[1]);
00081 kRP[2] = Rational(rkP[2]);
00082 int aiIndex[4] = { iV0, iV1, iV2, iV3 };
00083 Convert(4,aiIndex);
00084 return ToTetrahedron(kRP,iV0,iV1,iV2,iV3);
00085 }
00086
00087 template <class Real>
00088 int Query3TRational<Real>::ToCircumsphere (int i, int iV0, int iV1, int iV2,
00089 int iV3) const
00090 {
00091 int aiIndex[5] = { i, iV0, iV1, iV2, iV3 };
00092 Convert(5,aiIndex);
00093 return ToCircumsphere(m_akRVertex[i],iV0,iV1,iV2,iV3);
00094 }
00095
00096 template <class Real>
00097 int Query3TRational<Real>::ToCircumsphere (const Vector3<Real>& rkP, int iV0,
00098 int iV1, int iV2, int iV3) const
00099 {
00100 RVector kRP;
00101 kRP[0] = Rational(rkP[0]);
00102 kRP[1] = Rational(rkP[1]);
00103 kRP[2] = Rational(rkP[2]);
00104 int aiIndex[4] = { iV0, iV1, iV2, iV3 };
00105 Convert(4,aiIndex);
00106 return ToCircumsphere(kRP,iV0,iV1,iV2,iV3);
00107 }
00108
00109 template <class Real>
00110 void Query3TRational<Real>::Convert (int iQuantity, int* aiIndex) const
00111 {
00112 for (int i = 0; i < iQuantity; i++)
00113 {
00114 int j = aiIndex[i];
00115 if (!m_abEvaluated[j])
00116 {
00117 m_abEvaluated[j] = true;
00118 m_akRVertex[j][0] = Rational(m_akVertex[j][0]);
00119 m_akRVertex[j][1] = Rational(m_akVertex[j][1]);
00120 m_akRVertex[j][2] = Rational(m_akVertex[j][2]);
00121 }
00122 }
00123 }
00124
00125 template <class Real>
00126 int Query3TRational<Real>::ToPlane (const RVector& rkRP, int iV0, int iV1,
00127 int iV2) const
00128 {
00129 Rational kX0 = rkRP[0] - m_akRVertex[iV0][0];
00130 Rational kY0 = rkRP[1] - m_akRVertex[iV0][1];
00131 Rational kZ0 = rkRP[2] - m_akRVertex[iV0][2];
00132 Rational kX1 = m_akRVertex[iV1][0] - m_akRVertex[iV0][0];
00133 Rational kY1 = m_akRVertex[iV1][1] - m_akRVertex[iV0][1];
00134 Rational kZ1 = m_akRVertex[iV1][2] - m_akRVertex[iV0][2];
00135 Rational kX2 = m_akRVertex[iV2][0] - m_akRVertex[iV0][0];
00136 Rational kY2 = m_akRVertex[iV2][1] - m_akRVertex[iV0][1];
00137 Rational kZ2 = m_akRVertex[iV2][2] - m_akRVertex[iV0][2];
00138
00139 Rational kDet3 = Det3(kX0,kY0,kZ0,kX1,kY1,kZ1,kX2,kY2,kZ2);
00140 return (kDet3 > 0 ? +1 : (kDet3 < 0 ? -1 : 0));
00141 }
00142
00143 template <class Real>
00144 int Query3TRational<Real>::ToTetrahedron (const RVector& rkRP, int iV0,
00145 int iV1, int iV2, int iV3) const
00146 {
00147 int iSign0 = ToPlane(rkRP,iV1,iV2,iV3);
00148 if (iSign0 > 0)
00149 {
00150 return +1;
00151 }
00152
00153 int iSign1 = ToPlane(rkRP,iV0,iV2,iV3);
00154 if (iSign1 < 0)
00155 {
00156 return +1;
00157 }
00158
00159 int iSign2 = ToPlane(rkRP,iV0,iV1,iV3);
00160 if (iSign2 > 0)
00161 {
00162 return +1;
00163 }
00164
00165 int iSign3 = ToPlane(rkRP,iV0,iV1,iV2);
00166 if (iSign3 < 0)
00167 {
00168 return +1;
00169 }
00170
00171 return ((iSign0 && iSign1 && iSign2 && iSign3) ? -1 : 0);
00172 }
00173
00174 template <class Real>
00175 int Query3TRational<Real>::ToCircumsphere (const RVector& rkRP, int iV0,
00176 int iV1, int iV2, int iV3) const
00177 {
00178 RVector& rkRV0 = m_akRVertex[iV0];
00179 RVector& rkRV1 = m_akRVertex[iV1];
00180 RVector& rkRV2 = m_akRVertex[iV2];
00181 RVector& rkRV3 = m_akRVertex[iV3];
00182
00183 Rational kS0x = rkRV0[0] + rkRP[0];
00184 Rational kD0x = rkRV0[0] - rkRP[0];
00185 Rational kS0y = rkRV0[1] + rkRP[1];
00186 Rational kD0y = rkRV0[1] - rkRP[1];
00187 Rational kS0z = rkRV0[2] + rkRP[2];
00188 Rational kD0z = rkRV0[2] - rkRP[2];
00189 Rational kS1x = rkRV1[0] + rkRP[0];
00190 Rational kD1x = rkRV1[0] - rkRP[0];
00191 Rational kS1y = rkRV1[1] + rkRP[1];
00192 Rational kD1y = rkRV1[1] - rkRP[1];
00193 Rational kS1z = rkRV1[2] + rkRP[2];
00194 Rational kD1z = rkRV1[2] - rkRP[2];
00195 Rational kS2x = rkRV2[0] + rkRP[0];
00196 Rational kD2x = rkRV2[0] - rkRP[0];
00197 Rational kS2y = rkRV2[1] + rkRP[1];
00198 Rational kD2y = rkRV2[1] - rkRP[1];
00199 Rational kS2z = rkRV2[2] + rkRP[2];
00200 Rational kD2z = rkRV2[2] - rkRP[2];
00201 Rational kS3x = rkRV3[0] + rkRP[0];
00202 Rational kD3x = rkRV3[0] - rkRP[0];
00203 Rational kS3y = rkRV3[1] + rkRP[1];
00204 Rational kD3y = rkRV3[1] - rkRP[1];
00205 Rational kS3z = rkRV3[2] + rkRP[2];
00206 Rational kD3z = rkRV3[2] - rkRP[2];
00207 Rational kW0 = kS0x*kD0x + kS0y*kD0y + kS0z*kD0z;
00208 Rational kW1 = kS1x*kD1x + kS1y*kD1y + kS1z*kD1z;
00209 Rational kW2 = kS2x*kD2x + kS2y*kD2y + kS2z*kD2z;
00210 Rational kW3 = kS3x*kD3x + kS3y*kD3y + kS3z*kD3z;
00211 Rational kDet4 = Det4(kD0x,kD0y,kD0z,kW0,kD1x,kD1y,kD1z,kW1,kD2x,
00212 kD2y,kD2z,kW2,kD3x,kD3y,kD3z,kW3);
00213
00214 return (kDet4 > 0 ? 1 : (kDet4 < 0 ? -1 : 0));
00215 }
00216
00217 template <class Real>
00218 typename Query3TRational<Real>::Rational Query3TRational<Real>::Dot (
00219 Rational& rkX0, Rational& rkY0, Rational& rkZ0, Rational& rkX1,
00220 Rational& rkY1, Rational& rkZ1)
00221 {
00222 return rkX0*rkX1 + rkY0*rkY1 + rkZ0*rkZ1;
00223 }
00224
00225 template <class Real>
00226 typename Query3TRational<Real>::Rational Query3TRational<Real>::Det3 (
00227 Rational& rkX0, Rational& rkY0, Rational& rkZ0, Rational& rkX1,
00228 Rational& rkY1, Rational& rkZ1, Rational& rkX2, Rational& rkY2,
00229 Rational& rkZ2)
00230 {
00231 Rational kC00 = rkY1*rkZ2 - rkY2*rkZ1;
00232 Rational kC01 = rkY2*rkZ0 - rkY0*rkZ2;
00233 Rational kC02 = rkY0*rkZ1 - rkY1*rkZ0;
00234 return rkX0*kC00 + rkX1*kC01 + rkX2*kC02;
00235 }
00236
00237 template <class Real>
00238 typename Query3TRational<Real>::Rational Query3TRational<Real>::Det4 (
00239 Rational& rkX0, Rational& rkY0, Rational& rkZ0, Rational& rkW0,
00240 Rational& rkX1, Rational& rkY1, Rational& rkZ1, Rational& rkW1,
00241 Rational& rkX2, Rational& rkY2, Rational& rkZ2, Rational& rkW2,
00242 Rational& rkX3, Rational& rkY3, Rational& rkZ3, Rational& rkW3)
00243 {
00244 Rational kA0 = rkX0*rkY1 - rkX1*rkY0;
00245 Rational kA1 = rkX0*rkY2 - rkX2*rkY0;
00246 Rational kA2 = rkX0*rkY3 - rkX3*rkY0;
00247 Rational kA3 = rkX1*rkY2 - rkX2*rkY1;
00248 Rational kA4 = rkX1*rkY3 - rkX3*rkY1;
00249 Rational kA5 = rkX2*rkY3 - rkX3*rkY2;
00250 Rational kB0 = rkZ0*rkW1 - rkZ1*rkW0;
00251 Rational kB1 = rkZ0*rkW2 - rkZ2*rkW0;
00252 Rational kB2 = rkZ0*rkW3 - rkZ3*rkW0;
00253 Rational kB3 = rkZ1*rkW2 - rkZ2*rkW1;
00254 Rational kB4 = rkZ1*rkW3 - rkZ3*rkW1;
00255 Rational kB5 = rkZ2*rkW3 - rkZ3*rkW2;
00256 return kA0*kB5-kA1*kB4+kA2*kB3+kA3*kB2-kA4*kB1+kA5*kB0;
00257 }
00258
00259 }