File: LineSegment3D.h

package info (click to toggle)
primrose 6%2Bdfsg1-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster
  • size: 5,300 kB
  • ctags: 3,385
  • sloc: cpp: 27,318; php: 765; ansic: 636; objc: 272; sh: 136; makefile: 95; perl: 67
file content (212 lines) | stat: -rw-r--r-- 4,839 bytes parent folder | download | duplicates (18)
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
 * Modification History
 *
 * 2001-April-14   Jason Rohrer
 * Created.     
 *
 * 2005-February-15   Jason Rohrer
 * Made destructor virtual to quell warnings.
 */
 
 
#ifndef LINE_SEGMENT_3D_INCLUDED
#define LINE_SEGMENT_3D_INCLUDED 
 
#include "GeometricObject3D.h" 

#include "minorGems/io/Serializable.h"
 
#include <math.h>
#include <stdlib.h>
 
#ifndef M_PI
#define M_PI		3.14159265358979323846
#endif 
 
/**
 * Line segment (in 3-space). 
 *
 * @author Jason Rohrer 
 */
class LineSegment3D : public GeometricObject3D, public Serializable { 
	
	public:
	
		Vector3D *mEndpoints[2];

		/**
		 * Constructs a line segment.
		 *
		 * The vectors are copied, so the caller
		 * is responsible for deallocating the passed in vectors.
		 *
		 * @param inEndpointA the first endpoint.
		 * @param inEndpointB the second endpoint.
		 */
		LineSegment3D( Vector3D *inEndpointA, Vector3D *inEndpointB );
		
		
		/**
		 * Constructs a line segment by copying parameters
		 * from another line segment.
		 *
		 * @param inOtherSegment the line segment to copy.
		 */
		LineSegment3D( LineSegment3D *inOtherSegment );
		
		
		virtual ~LineSegment3D();
		

		/**
		 * Gets the projection of a point onto this line segment
		 *
		 * @param inPoint the point to project.  Must be destroyed
		 *   by caller.
		 *
		 * @return a new vector representing the projected point, or
		 *   NULL if the projection is not on this line segment.
		 */
		Vector3D *projectPoint( Vector3D *inPoint );

		
		// these implement the GeometricObject3D interface
		void move( Vector3D *inVector );
		void rotate( Angle3D *inAngle );
		void reverseRotate( Angle3D *inAngle );
		void scale( double inScalar );
		GeometricObject3D *copy();
		
		// implement the Serializable interface
		virtual int serialize( OutputStream *inOutputStream );
		virtual int deserialize( InputStream *inInputStream );

	};



inline LineSegment3D::LineSegment3D( Vector3D *inEndpointA,
									 Vector3D *inEndpointB ) {
	mEndpoints[0] = new Vector3D( inEndpointA );
	mEndpoints[1] = new Vector3D( inEndpointB );
	}



inline LineSegment3D::LineSegment3D( LineSegment3D *inOtherSegment ) {

	mEndpoints[0] = new Vector3D( inOtherSegment->mEndpoints[0] );
	mEndpoints[1] = new Vector3D( inOtherSegment->mEndpoints[1] );

	}



inline LineSegment3D::~LineSegment3D() {
	delete mEndpoints[0];
	delete mEndpoints[1];
	}



inline Vector3D *LineSegment3D::projectPoint( Vector3D *inPoint ) {

	// compute the vector for the underlying line of this segment
	Vector3D *supportingVector = new Vector3D( mEndpoints[0] );
	supportingVector->subtract( mEndpoints[1] );

	supportingVector->normalize();
	
	// now find the lengths of the projection of each endpoint
	// onto this supporting vector
	double lengthA = supportingVector->dot( mEndpoints[0] );
	double lengthB = supportingVector->dot( mEndpoints[1] );
	
	// find the length of the projection of inPoint
	double lengthInPoint = supportingVector->dot( inPoint );

	// projection is off the segment if lengthInPoint is not
	// between lengthA and lengthB
	if( lengthInPoint > lengthA && lengthInPoint > lengthB ) {
		delete supportingVector;
		return NULL;
		}
	else if( lengthInPoint < lengthA && lengthInPoint < lengthB ) {
		delete supportingVector;
		return NULL;
		}
	else {
		// length is in between the two

		// compute difference from A's projection
		double difference = lengthInPoint - lengthA;
		Vector3D *returnVector = new Vector3D( mEndpoints[0] );

		supportingVector->scale( difference );
		returnVector->add( supportingVector );

		delete supportingVector;
		return returnVector;
		}
	}



inline void LineSegment3D::move( Vector3D *inVector ) {
	mEndpoints[0]->add( inVector );
	mEndpoints[1]->add( inVector );
	}
	
	
	
inline void LineSegment3D::rotate( Angle3D *inAngle ) {
	mEndpoints[0]->rotate( inAngle );
	mEndpoints[1]->rotate( inAngle );
	}
	
	
	
inline void LineSegment3D::reverseRotate( Angle3D *inAngle ) {
	mEndpoints[0]->reverseRotate( inAngle );
	mEndpoints[1]->reverseRotate( inAngle );
	}


	
inline void LineSegment3D::scale( double inScalar ) {
	mEndpoints[0]->scale( inScalar );
	mEndpoints[1]->scale( inScalar );
	}
	
	
	
inline GeometricObject3D *LineSegment3D::copy() {
	LineSegment3D *copiedSegment = new LineSegment3D( this );
	return (GeometricObject3D*)copiedSegment;
	}



inline int LineSegment3D::serialize( OutputStream *inOutputStream ) {
	int numBytesWritten = 0;
	
	numBytesWritten += mEndpoints[0]->serialize( inOutputStream );
	numBytesWritten += mEndpoints[1]->serialize( inOutputStream );

	return numBytesWritten;
	}
	
	
	
inline int LineSegment3D::deserialize( InputStream *inInputStream ) {
	int numBytesRead = 0;

	numBytesRead += mEndpoints[0]->deserialize( inInputStream );
	numBytesRead += mEndpoints[1]->deserialize( inInputStream );
	
	return numBytesRead;
	}
	
	
	
#endif