File: Wm5IntrPlane3Cylinder3.cpp

package info (click to toggle)
libwildmagic 5.17%2Bcleaned1-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 90,124 kB
  • sloc: cpp: 215,940; csh: 637; sh: 91; makefile: 40
file content (185 lines) | stat: -rw-r--r-- 6,723 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// Geometric Tools, LLC
// Copyright (c) 1998-2014
// Distributed under the Boost Software License, Version 1.0.
// http://www.boost.org/LICENSE_1_0.txt
// http://www.geometrictools.com/License/Boost/LICENSE_1_0.txt
//
// File Version: 5.0.2 (2014/02/14)

#include "Wm5MathematicsPCH.h"
#include "Wm5IntrPlane3Cylinder3.h"

namespace Wm5
{
//----------------------------------------------------------------------------
template <typename Real>
IntrPlane3Cylinder3<Real>::IntrPlane3Cylinder3 (const Plane3<Real>& rkPlane,
    const Cylinder3<Real>& rkCylinder)
    :
    mPlane(&rkPlane),
    mCylinder(&rkCylinder)
{
    mType = PC_EMPTY_SET;
}
//----------------------------------------------------------------------------
template <typename Real>
const Plane3<Real>& IntrPlane3Cylinder3<Real>::GetPlane () const
{
    return *mPlane;
}
//----------------------------------------------------------------------------
template <typename Real>
const Cylinder3<Real>& IntrPlane3Cylinder3<Real>::GetCylinder () const
{
    return *mCylinder;
}
//----------------------------------------------------------------------------
template <typename Real>
bool IntrPlane3Cylinder3<Real>::Test ()
{
    // Compute extremes of signed distance Dot(N,X)-d for points on the
    // cylinder.  These are
    //   min = (Dot(N,C)-d) - r*sqrt(1-Dot(N,W)^2) - (h/2)*|Dot(N,W)|
    //   max = (Dot(N,C)-d) + r*sqrt(1-Dot(N,W)^2) + (h/2)*|Dot(N,W)|
    Real sDist = mPlane->DistanceTo(mCylinder->Axis.Origin);
    Real absNdW = Math<Real>::FAbs(mPlane->Normal.Dot(
        mCylinder->Axis.Direction));
    Real root = Math<Real>::Sqrt(Math<Real>::FAbs((Real)1 - absNdW*absNdW));
    Real term = mCylinder->Radius*root +
        ((Real)0.5)*mCylinder->Height*absNdW;

    // Intersection occurs if and only if 0 is in the interval [min,max].
    return Math<Real>::FAbs(sDist) <= term;
}
//----------------------------------------------------------------------------
template <typename Real>
bool IntrPlane3Cylinder3<Real>::Find ()
{
    Real sDist = mPlane->DistanceTo(mCylinder->Axis.Origin);
    Vector3<Real> center =  mCylinder->Axis.Origin -
        sDist*mPlane->Normal;
    Real cosTheta = mCylinder->Axis.Direction.Dot(mPlane->Normal);
    Real absCosTheta = Math<Real>::FAbs(cosTheta);

    if (absCosTheta > (Real)0)
    {
        // The cylinder axis intersects the plane in a unique point.
        if (absCosTheta < (Real)1)
        {
            mType = PC_ELLIPSE;
            mEllipse.Normal = mPlane->Normal;
            mEllipse.Center = mCylinder->Axis.Origin -
                (sDist/cosTheta)*mCylinder->Axis.Direction;
            mEllipse.Major = mCylinder->Axis.Direction -
                cosTheta*mPlane->Normal;
            mEllipse.Minor = mPlane->Normal.Cross(mEllipse.Major);
            mEllipse.MajorLength = mCylinder->Radius/absCosTheta;
            mEllipse.MinorLength = mCylinder->Radius;
            mEllipse.Major.Normalize();
            mEllipse.Minor.Normalize();
            return true;
        }
        else
        {
            mType = PC_CIRCLE;
            mCircle.Normal = mPlane->Normal;
            mCircle.Center = center;
            mCircle.Radius = mCylinder->Radius;
            return true;
        }
    }
    else
    {
        // The cylinder is parallel to the plane.
        Real absSDist = Math<Real>::FAbs(sDist);
        if (absSDist < mCylinder->Radius)
        {
            mType = PC_TWO_LINES;

            Vector3<Real> offset = mCylinder->Axis.Direction.Cross(
                mPlane->Normal);
            Real extent = Math<Real>::Sqrt(
                mCylinder->Radius*mCylinder->Radius - sDist*sDist);

            mLine0.Origin = center - extent*offset;
            mLine0.Direction = mCylinder->Axis.Direction;
            mLine1.Origin = center + extent*offset;
            mLine1.Direction = mCylinder->Axis.Direction;
            return true;
        }
        else if (absSDist == mCylinder->Radius)
        {
            mType = PC_ONE_LINE;
            mLine0.Origin = center;
            mLine0.Direction = mCylinder->Axis.Direction;
            return true;
        }
        else
        {
            mType = PC_EMPTY_SET;
            return false;
        }
    }
}
//----------------------------------------------------------------------------
template <typename Real>
bool IntrPlane3Cylinder3<Real>::CylinderIsCulled () const
{
    // Compute extremes of signed distance Dot(N,X)-d for points on the
    // cylinder.  These are
    //   min = (Dot(N,C)-d) - r*sqrt(1-Dot(N,W)^2) - (h/2)*|Dot(N,W)|
    //   max = (Dot(N,C)-d) + r*sqrt(1-Dot(N,W)^2) + (h/2)*|Dot(N,W)|
    Real sDist = mPlane->DistanceTo(mCylinder->Axis.Origin);
    Real absNdW = Math<Real>::FAbs(mPlane->Normal.Dot(
        mCylinder->Axis.Direction));
    Real root = Math<Real>::Sqrt(Math<Real>::FAbs((Real)1 - absNdW*absNdW));
    Real term = mCylinder->Radius*root +
        ((Real)0.5)*mCylinder->Height*absNdW;

    // Culling occurs if and only if max <= 0.
    return sDist + term <= (Real)0;
}
//----------------------------------------------------------------------------
template <typename Real>
int IntrPlane3Cylinder3<Real>::GetType () const
{
    return mType;
}
//----------------------------------------------------------------------------
template <typename Real>
void IntrPlane3Cylinder3<Real>::GetOneLine (Line3<Real>& line) const
{
    line = mLine0;
}
//----------------------------------------------------------------------------
template <typename Real>
void IntrPlane3Cylinder3<Real>::GetTwoLines (Line3<Real>& line0,
    Line3<Real>& line1) const
{
    line0 = mLine0;
    line1 = mLine1;
}
//----------------------------------------------------------------------------
template <typename Real>
void IntrPlane3Cylinder3<Real>::GetCircle (Circle3<Real>& circle) const
{
    circle = mCircle;
}
//----------------------------------------------------------------------------
template <typename Real>
void IntrPlane3Cylinder3<Real>::GetEllipse (Ellipse3<Real>& ellipse) const
{
    ellipse = mEllipse;
}
//----------------------------------------------------------------------------

//----------------------------------------------------------------------------
// Explicit instantiation.
//----------------------------------------------------------------------------
template WM5_MATHEMATICS_ITEM
class IntrPlane3Cylinder3<float>;

template WM5_MATHEMATICS_ITEM
class IntrPlane3Cylinder3<double>;
//----------------------------------------------------------------------------
}