Simbody  3.7
BicubicSurface.h
Go to the documentation of this file.
1 #ifndef SimTK_BICUBIC_SURFACE_H_
2 #define SimTK_BICUBIC_SURFACE_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: Matthew Millard, 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 
31 #include "SimTKcommon.h"
33 #include "simmath/internal/Geo.h"
36 
37 #include <limits>
38 
39 namespace SimTK {
40 
41 //==============================================================================
42 // CLASS BICUBIC SURFACE
43 //==============================================================================
159 public:
160  class PatchHint; // See below for definition of PatchHint.
161 
164  BicubicSurface() : guts(0) {}
176 
177 
201  BicubicSurface(const Vector& x, const Vector& y, const Matrix& f,
202  Real smoothness=0);
203 
231  BicubicSurface(const Vec2& XY, const Vec2& spacing,
232  const Matrix& f, Real smoothness=0);
233 
247  Real calcValue(const Vec2& XY, PatchHint& hint) const;
248 
252  Real calcValue(const Vec2& XY) const;
253 
266  UnitVec3 calcUnitNormal(const Vec2& XY, PatchHint& hint) const;
267 
271  UnitVec3 calcUnitNormal(const Vec2& XY) const;
272 
298  Real calcDerivative(const Array_<int>& derivComponents,
299  const Vec2& XY, PatchHint& hint) const;
300 
304  Real calcDerivative(const Array_<int>& derivComponents,
305  const Vec2& XY) const;
306 
318  bool isSurfaceDefined(const Vec2& XY) const;
319 
322  Vec2 getMinXY() const;
325  Vec2 getMaxXY() const;
326 
336 
337  //--------------------------------------------------------------------------
347  int getNumAccesses() const;
371  void resetStatistics() const;
375  //--------------------------------------------------------------------------
393  BicubicSurface(const Vector& x, const Vector& y, const Matrix& f,
394  const Matrix& fx, const Matrix& fy, const Matrix& fxy);
396  BicubicSurface(const Vec2& XY, const Vec2& spacing, const Matrix& f,
397  const Matrix& fx, const Matrix& fy, const Matrix& fxy);
398 
423  void calcParaboloid(const Vec2& XY, PatchHint& hint,
424  Transform& X_SP, Vec2& k) const;
428  void calcParaboloid(const Vec2& XY, Transform& X_SP, Vec2& k) const;
429 
433  void getNumPatches(int& nx, int& ny) const;
434 
439 
446  //--------------------------------------------------------------------------
455  bool isEmpty() const {return guts==0;}
456 
462  void clear(); // Hide from Doxygen.
466  class Guts; // Opaque implementation class.
467  const BicubicSurface::Guts& getGuts() const
468  { assert(guts); return *guts; }
470 private:
471  BicubicSurface::Guts* guts;
472 }; // END class BicubicSurface
473 
474 
475 
476 //==============================================================================
477 // CLASS BICUBIC FUNCTION :: PATCH HINT
478 //==============================================================================
483 public:
489  PatchHint(const PatchHint& source);
493  PatchHint& operator=(const PatchHint& source);
496 
499  bool isEmpty() const;
502  void clear();
503  // Hide from Doxygen
505  class Guts; // Hidden implementation of PatchHint.
506  const Guts& getGuts() const {return *guts;}
507  Guts& updGuts() {return *guts;}
509 private:
510  Guts* guts;
511 };
512 
513 
514 
515 //==============================================================================
516 // CLASS BICUBIC FUNCTION
517 //==============================================================================
518 
533 public:
536  BicubicFunction(const BicubicSurface& surface) : surface(surface) {}
537 
540  const BicubicSurface& getBicubicSurface() const {return surface;}
541 
549  Real calcValue(const Vector& XY) const override {
550  SimTK_ERRCHK1(XY.size()==2, "BicubicFunction::calcValue()",
551  "The argument Vector XY must have exactly 2 elements but had %d.",
552  XY.size());
553  return surface.calcValue(Vec2(XY[0],XY[1]), hint);
554  }
555 
575  Real calcDerivative(const Array_<int>& derivComponents,
576  const Vector& XY) const override {
577  SimTK_ERRCHK1(XY.size()==2, "BicubicFunction::calcDerivative()",
578  "The argument Vector XY must have exactly 2 elements but had %d.",
579  XY.size());
580  return surface.calcDerivative(derivComponents, Vec2(XY[0],XY[1]), hint);
581  }
582 
585  int getArgumentSize() const override {return 2;}
586 
592  int getMaxDerivativeOrder() const override
593  { return std::numeric_limits<int>::max(); }
594 
595  BicubicFunction* clone() const override {return new BicubicFunction(*this);}
596 
599  Real calcDerivative(const std::vector<int>& derivComponents,
600  const Vector& x) const
601  { return calcDerivative(ArrayViewConst_<int>(derivComponents),x); }
602 
603 private:
604  BicubicSurface surface;
605  mutable BicubicSurface::PatchHint hint;
606 };
607 
608 
609 
610 }; //namespace
611 //=============================================================================
612 //=============================================================================
613 
614 #endif // SimTK_BICUBIC_SURFACE_H_
SimTK::BicubicSurface::getNumAccessesNearbyPatch
int getNumAccessesNearbyPatch() const
This is the number of accesses which specified on a point that was not on the patch currently in the ...
SimTK::Vec2
Vec< 2 > Vec2
This is the most common 2D vector type: a column of 2 Real values stored consecutively in memory (pac...
Definition: SmallMatrix.h:126
SimTK::BicubicFunction::clone
BicubicFunction * clone() const override
Definition: BicubicSurface.h:595
SimTK::BicubicSurface::resetStatistics
void resetStatistics() const
Reset all statistics to zero.
SimTK::Function_
This abstract class represents a mathematical function that calculates a value of arbitrary type base...
Definition: Function.h:51
SimTK::BicubicSurface::PatchHint
This object is used to hold precalculated data about the most recently accessed patch to accelerate t...
Definition: BicubicSurface.h:482
SimTK::BicubicSurface::getNumAccessesSamePatch
int getNumAccessesSamePatch() const
This is the number of accesses which specified a new point on the same patch as was already present i...
SimTK::BicubicSurface::BicubicSurface
BicubicSurface(const BicubicSurface &source)
Copy constructor makes a shallow copy of source; the new handle will reference the same underlying su...
SimTK::BicubicSurface::BicubicSurface
BicubicSurface()
Construct an uninitialized BicubicSurface handle.
Definition: BicubicSurface.h:164
SimTK_SIMMATH_EXPORT
#define SimTK_SIMMATH_EXPORT
Definition: SimTKmath/include/simmath/internal/common.h:64
SimTK::BicubicFunction::calcDerivative
Real calcDerivative(const Array_< int > &derivComponents, const Vector &XY) const override
Calculate a partial derivative of this function at a particular point.
Definition: BicubicSurface.h:575
SimTK_ERRCHK1
#define SimTK_ERRCHK1(cond, whereChecked, fmt, a1)
Definition: ExceptionMacros.h:326
SimTK
This is a System that represents the dynamics of a particle moving along a smooth surface.
Definition: Assembler.h:37
SimTK::BicubicFunction::getBicubicSurface
const BicubicSurface & getBicubicSurface() const
Return a reference to the BicubicSurface object being used by this BicubicFunction.
Definition: BicubicSurface.h:540
Geo_BicubicBezierPatch.h
SimTK::BicubicSurface::calcDerivative
Real calcDerivative(const Array_< int > &derivComponents, const Vec2 &XY) const
This is a slow-but-convenient version of calcDerivative() since it does not provide for a PatchHint.
SimTK::UnitVec< Real, 1 >
SimTKcommon.h
SimTK::Vec< 2 >
common.h
SimTK::BicubicSurface::createPolygonalMesh
PolygonalMesh createPolygonalMesh(Real resolution=1) const
Create a mesh that can be used to visualize this surface.
SimTK::BicubicSurface::PatchHint::PatchHint
PatchHint(const PatchHint &source)
Copy an existing PatchHint to create a new one with the same contents.
SimTK::BicubicSurface::~BicubicSurface
~BicubicSurface()
Destructor deletes the underlying surface if there are no more handles referencing it,...
SimTK::BicubicSurface::calcParaboloid
void calcParaboloid(const Vec2 &XY, PatchHint &hint, Transform &X_SP, Vec2 &k) const
(Advanced) For use with Hertz contact at a point Q we need to know the surface normal and principal c...
SimTK::BicubicFunction::getMaxDerivativeOrder
int getMaxDerivativeOrder() const override
This implements the Function base class pure virtual specifying how many derivatives can be taken of ...
Definition: BicubicSurface.h:592
SimTK::BicubicSurface::calcValue
Real calcValue(const Vec2 &XY, PatchHint &hint) const
Calculate the value of the surface at a particular XY coordinate.
SimTK::Geo::BicubicHermitePatch_
A primitive useful for computations involving a single bicubic Hermite patch.
Definition: Geo.h:66
SimTK::BicubicSurface::isEmpty
bool isEmpty() const
Return true if this is an empty handle meaning that it does not currently refer to any surface.
Definition: BicubicSurface.h:455
SimTK::BicubicSurface::calcUnitNormal
UnitVec3 calcUnitNormal(const Vec2 &XY, PatchHint &hint) const
Calculate the outward unit normal to the surface at a particular XY coordinate.
SimTK::BicubicSurface::BicubicSurface
BicubicSurface(const Vector &x, const Vector &y, const Matrix &f, const Matrix &fx, const Matrix &fy, const Matrix &fxy)
(Advanced) A constructor for a bicubic surface that sets the partial derivatives of the surface to th...
SimTK::max
ELEM max(const VectorBase< ELEM > &v)
Definition: VectorMath.h:251
Geo.h
SimTK::PolygonalMesh
This class provides a description of a mesh made of polygonal faces (not limited to triangles).
Definition: PolygonalMesh.h:71
Geo_BicubicHermitePatch.h
SimTK::ArrayViewConst_
This Array_ helper class is the base class for ArrayView_ which is the base class for Array_; here we...
Definition: Array.h:51
SimTK::BicubicSurface::PatchHint::PatchHint
PatchHint()
Creates an empty PatchHint, meaning it contains no meaningful hint information.
SimTK::BicubicSurface::isSurfaceDefined
bool isSurfaceDefined(const Vec2 &XY) const
The surface interpolation only works within the grid defined by the vectors x and y used in the const...
SimTK::BicubicSurface::BicubicSurface
BicubicSurface(const Vector &x, const Vector &y, const Matrix &f, Real smoothness=0)
Construct a bicubic surface that approximates F(X,Y) given samples f(i,j) with the sample locations i...
SimTK::BicubicSurface::getNumPatches
void getNumPatches(int &nx, int &ny) const
(Advanced) Get the number of individual bicubic patches used to form this surface,...
SimTK::BicubicSurface::calcBezierPatch
Geo::BicubicBezierPatch calcBezierPatch(int x, int y) const
(Advanced) Select a patch by its (x,y) position in the rectangular grid of individual bicubic patches...
SimTK::BicubicFunction::calcValue
Real calcValue(const Vector &XY) const override
Calculate the value of the function at a particular XY coordinate.
Definition: BicubicSurface.h:549
SimTK::BicubicFunction
This is a two-argument Function built using a shared BicubicSurface and managing current state to opt...
Definition: BicubicSurface.h:532
SimTK::BicubicSurface::calcHermitePatch
Geo::BicubicHermitePatch calcHermitePatch(int x, int y) const
(Advanced) Select a patch by its (x,y) position in the rectangular grid of individual bicubic patches...
SimTK::Array_< int >
SimTK::BicubicSurface::calcParaboloid
void calcParaboloid(const Vec2 &XY, Transform &X_SP, Vec2 &k) const
(Advanced) This is a slow-but-convenient version of calcParaboloid() since it does not provide for a ...
SimTK::Real
SimTK_Real Real
This is the default compiled-in floating point type for SimTK, either float or double.
Definition: SimTKcommon/include/SimTKcommon/internal/common.h:606
SimTK::BicubicSurface::BicubicSurface
BicubicSurface(const Vec2 &XY, const Vec2 &spacing, const Matrix &f, Real smoothness=0)
Construct a bicubic surface that approximates F(X,Y) given samples f(i,j) over a grid with regular sp...
SimTK::BicubicSurface::calcDerivative
Real calcDerivative(const Array_< int > &derivComponents, const Vec2 &XY, PatchHint &hint) const
Calculate a partial derivative of this function at a particular point.
SimTK::BicubicSurface::PatchHint::isEmpty
bool isEmpty() const
Return true if this object currently contains no meaningful hint information.
SimTK::BicubicSurface::calcValue
Real calcValue(const Vec2 &XY) const
This is a slow-but-convenient version of calcValue() since it does not provide for a PatchHint.
SimTK::BicubicSurface::getNumAccessesSamePoint
int getNumAccessesSamePoint() const
This is the number of accesses which specified a point whose information was already available in the...
SimTK::BicubicFunction::getArgumentSize
int getArgumentSize() const override
This implements the Function base class pure virtual; here it always returns 2 (X and Y).
Definition: BicubicSurface.h:585
SimTK::Vector_< Real >
SimTK::BicubicSurface::calcUnitNormal
UnitVec3 calcUnitNormal(const Vec2 &XY) const
This is a slow-but-convenient version of calcUnitNormal() since it does not provide for a PatchHint.
SimTK::BicubicSurface::getMaxXY
Vec2 getMaxXY() const
Return the highest XY pair for which this surface is defined; that is the point (xmax,...
SimTK::BicubicFunction::BicubicFunction
BicubicFunction(const BicubicSurface &surface)
Create a BicubicFunction referencing the given BicubicSurface, which is shared not copied.
Definition: BicubicSurface.h:536
SimTK::BicubicSurface::PatchHint::~PatchHint
~PatchHint()
Destruct this PatchHint.
SimTK::BicubicSurface::BicubicSurface
BicubicSurface(const Vec2 &XY, const Vec2 &spacing, const Matrix &f, const Matrix &fx, const Matrix &fy, const Matrix &fxy)
(Advanced) Same, but with regular grid spacing.
SimTK::BicubicSurface::PatchHint::operator=
PatchHint & operator=(const PatchHint &source)
Set the contents of this PatchHint to be the same as that of source.
SimTK::Transform_< Real >
SimTK::BicubicSurface
This class will create a smooth surface that approximates a two-argument function F(X,...
Definition: BicubicSurface.h:158
SimTK::Matrix_< Real >
SimTK::BicubicSurface::clear
void clear()
Return this handle to its default-constructed state, meaning that it will not refer to any surface.
SimTK::BicubicFunction::calcDerivative
Real calcDerivative(const std::vector< int > &derivComponents, const Vector &x) const
This provides compatibility with std::vector without requiring any copying.
Definition: BicubicSurface.h:599
SimTK::Geo::BicubicBezierPatch_
A primitive useful for computations involving a single bicubic Bezier patch.
Definition: Geo.h:68
SimTK::BicubicSurface::PatchHint::clear
void clear()
Erase any information currently stored in this PatchHint.
SimTK::BicubicSurface::getNumAccesses
int getNumAccesses() const
This is the total number of calls made to either calcValue() or calcDerivative().
SimTK::BicubicSurface::operator=
BicubicSurface & operator=(const BicubicSurface &source)
Copy assignment is shallow; it makes this handle reference the same underlying surface as does source...
SimTK::BicubicSurface::getMinXY
Vec2 getMinXY() const
Return the lowest XY pair for which this surface is defined; that is the point (xmin,...