Simbody  3.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Geo_LineSeg.h
Go to the documentation of this file.
1 #ifndef SimTK_SIMMATH_GEO_LINESEG_H_
2 #define SimTK_SIMMATH_GEO_LINESEG_H_
3 
4 /* -------------------------------------------------------------------------- *
5  * Simbody(tm): SimTKmath *
6  * -------------------------------------------------------------------------- *
7  * This is part of the SimTK biosimulation toolkit originating from *
8  * Simbios, the NIH National Center for Physics-Based Simulation of *
9  * Biological Structures at Stanford, funded under the NIH Roadmap for *
10  * Medical Research, grant U54 GM072970. See https://simtk.org/home/simbody. *
11  * *
12  * Portions copyright (c) 2011-12 Stanford University and the Authors. *
13  * Authors: Michael Sherman *
14  * Contributors: *
15  * *
16  * Licensed under the Apache License, Version 2.0 (the "License"); you may *
17  * not use this file except in compliance with the License. You may obtain a *
18  * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. *
19  * *
20  * Unless required by applicable law or agreed to in writing, software *
21  * distributed under the License is distributed on an "AS IS" BASIS, *
22  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
23  * See the License for the specific language governing permissions and *
24  * limitations under the License. *
25  * -------------------------------------------------------------------------- */
26 
30 #include "SimTKcommon.h"
32 #include "simmath/internal/Geo.h"
34 
35 #include <cassert>
36 #include <cmath>
37 #include <algorithm>
38 
39 namespace SimTK {
40 
41 
42 //==============================================================================
43 // GEO LINESEG
44 //==============================================================================
49 template <class P>
50 class SimTK_SIMMATH_EXPORT Geo::LineSeg_ {
51 typedef P RealP;
52 typedef Vec<3,RealP> Vec3P;
53 
54 public:
58 
61 LineSeg_(const Vec3P& e0, const Vec3P& e1)
62 { setEndpoints(e0,e1); }
63 
65 LineSeg_& setEndpoints(const Vec3P& e0, const Vec3P& e1)
66 { e[0]=e0; e[1]=e1; return *this; }
67 
69 LineSeg_& setEndpoint(int which, const Vec3P& p)
70 { assert(which==0 || which==1); e[which] = p; return *this; }
71 
77 bool isDegenerate(RealP tol = Geo::getDefaultTol<P>()) const
78 { assert(tol >= 0); return calcLengthSqr() <= square(tol); }
79 
82 const Vec3P& getEndpoint(int which) const
83 { assert(which==0 || which==1); return e[which]; }
84 
88 Vec3P& updEndpoint(int which)
89 { assert(which==0 || which==1); return e[which]; }
90 
92 const Vec3P& operator[](int which) const {return getEndpoint(which);}
94 Vec3P& operator[](int which) {return updEndpoint(which);}
95 
98 RealP calcLength() const {return (e[1]-e[0]).norm(); }
99 
102 RealP calcLengthSqr() const {return (e[1]-e[0]).normSqr(); }
103 
108 Vec3P findPoint(RealP t) const
109 { return t*e[0] + (1-t)*e[1]; }
110 
114 { return (e[0]+e[1]) / RealP(2); }
115 
120 { return Geo::Point_<P>::calcBoundingSphere(e[0],e[1]); }
121 
124 RealP findDistanceToPoint(const Vec3P& p2) const
125 {SimTK_ASSERT_ALWAYS(!"implemented",
126 "Geo::LineSeg_::findDistanceToPoint(): Not implemented yet.");
127 return Geo::getNaN<P>();}
128 
131 RealP findDistanceToPointSqr(const Vec3P& p2) const
132 {SimTK_ASSERT_ALWAYS(!"implemented",
133 "Geo::LineSeg_::findDistanceToPointSqr(): Not implemented yet.");
134 return Geo::getNaN<P>();}
135 
142 private:
143 Vec3P e[2];
144 };
145 
146 
147 } // namespace SimTK
148 
149 #endif // SimTK_SIMMATH_GEO_LINESEG_H_
RealP calcLength() const
Calculate the length of this line segment (expensive).
Definition: Geo_LineSeg.h:98
LineSeg_ & setEndpoints(const Vec3P &e0, const Vec3P &e1)
Change the end points of this line segment.
Definition: Geo_LineSeg.h:65
#define SimTK_ASSERT_ALWAYS(cond, msg)
Definition: ExceptionMacros.h:349
RealP findDistanceToPointSqr(const Vec3P &p2) const
Find the square of the distance between this line segment and a point expressed in the same frame...
Definition: Geo_LineSeg.h:131
const Vec3P & operator[](int which) const
Access an end point by indexing the line segment.
Definition: Geo_LineSeg.h:92
unsigned char square(unsigned char u)
Definition: Scalar.h:351
Vec3P & operator[](int which)
Get writable access to an end point by indexing the line segment.
Definition: Geo_LineSeg.h:94
Vec3P & updEndpoint(int which)
Get a writable reference to the location of an end point. Order is the same as construction.
Definition: Geo_LineSeg.h:88
RealP findDistanceToPoint(const Vec3P &p2) const
Find the distance between this line segment and a point expressed in the same frame.
Definition: Geo_LineSeg.h:124
Includes internal headers providing declarations for the basic SimTK Core classes, including Simmatrix.
Defines geometric primitive shapes and algorthms.
A 3d line segment primitive represented by its end points in an unspecified frame, and a collection of line segment-related utility methods.
Definition: Geo.h:57
Vec3P findMidpoint() const
Return the center point of the line segment; this is the same as findPoint(1/2) but faster...
Definition: Geo_LineSeg.h:113
This is the header file that every Simmath compilation unit should include first. ...
LineSeg_()
Construct an uninitialized LineSeg object; the end points will be garbage (NaN in Debug builds)...
Definition: Geo_LineSeg.h:57
RealP calcLengthSqr() const
Calculate the square of the length of this line segment (cheap).
Definition: Geo_LineSeg.h:102
Sphere_< P > calcBoundingSphere() const
Calculate a minimal bounding sphere for this line segment.
Definition: Geo_LineSeg.h:119
A geometric primitive representing a sphere by its radius and center point, and a collection of spher...
Definition: Geo.h:56
Vec3P findPoint(RealP t) const
Return a point along the line segment given by its coordinate t, where t==0 gives end point 0...
Definition: Geo_LineSeg.h:108
LineSeg_ & setEndpoint(int which, const Vec3P &p)
Change one end point of this line segment.
Definition: Geo_LineSeg.h:69
LineSeg_(const Vec3P &e0, const Vec3P &e1)
Construct a LineSeg with the given end points.
Definition: Geo_LineSeg.h:61
Defines primitive operations on spheres.
#define SimTK_SIMMATH_EXPORT
Definition: SimTKmath/include/simmath/internal/common.h:64
bool isDegenerate(RealP tol=Geo::getDefaultTol< P >()) const
Determine whether this line segment is degenerate to a given tolerance, meaning that its length is le...
Definition: Geo_LineSeg.h:77
const Vec3P & getEndpoint(int which) const
Get the location of an end point. Order is the same as construction.
Definition: Geo_LineSeg.h:82
static Sphere_< P > calcBoundingSphere(const Vec3P &p)
Create a tiny bounding sphere around a single point.
Definition: Geo_Point.h:333