BSplineData.h 5.76 KB
Newer Older
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
/*
Copyright (c) 2006, Michael Kazhdan and Matthew Bolitho
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

Redistributions of source code must retain the above copyright notice, this list of
conditions and the following disclaimer. Redistributions in binary form must reproduce
the above copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the distribution. 

Neither the name of the Johns Hopkins University nor the names of its contributors
may be used to endorse or promote products derived from this software without specific
prior written permission. 

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO THE IMPLIED WARRANTIES 
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
TO, PROCUREMENT OF SUBSTITUTE  GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
DAMAGE.
*/

#ifndef BSPLINE_DATA_INCLUDED
#define BSPLINE_DATA_INCLUDED


#include "PPolynomial.h"
#include "Array.h"

36
template< int Degree >
37
38
struct BSplineElementCoefficients
{
39
40
41
42
  int coeffs[Degree+1];
  BSplineElementCoefficients( void ) { memset( coeffs , 0 , sizeof( int ) * ( Degree+1 ) ); }
  int& operator[]( unsigned int idx ){ if (idx <= Degree) return coeffs[idx]; return coeffs[0];}
  const int& operator[]( unsigned int idx ) const {if (idx <= Degree) return coeffs[idx]; return coeffs[0];}
43
44
45
46
};
template< int Degree >
struct BSplineElements : public std::vector< BSplineElementCoefficients< Degree > >
{
47
48
49
  static const int _off = (Degree+1)/2;
  void _addLeft ( int offset , int boundary );
  void _addRight( int offset , int boundary );
50
public:
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
  enum
  {
    NONE      =  0,
    DIRICHLET = -1,
    NEUMANN   =  1
  };
  // Coefficients are ordered as "/" "-" "\"
  int denominator;

  BSplineElements( void ) { denominator = 1; }
  BSplineElements( int res , int offset , int boundary=NONE , int inset=0 );

  void upSample( BSplineElements& high ) const;
  void differentiate( BSplineElements< Degree-1 >& d ) const;

  void print( FILE* fp=stdout ) const
  {
68
        for( int i=0 ; i< this->size() ; i++ )
69
70
71
72
73
74
    {
      printf( "%d]" , i );
      for( int j=0 ; j<=Degree ; j++ ) printf( " %d" , (*this)[i][j] );
      printf( " (%d)\n" , denominator );
    }
  }
75
76
77
78
79
};

template< int Degree , class Real >
class BSplineData
{
80
81
  bool useDotRatios;
  int boundaryType;
82
public:
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
  struct BSplineComponents
  {
    Polynomial< Degree > polys[Degree+1];
    Polynomial< Degree >& operator[] ( unsigned int idx ) { return polys[idx]; }
    const Polynomial< Degree >& operator[] ( unsigned int idx ) const { return polys[idx]; }
    void printnl( void ) const  { for( int d=0 ; d<=Degree ; d++ ) polys[d].printnl(); }
    BSplineComponents scale( double s ) const { BSplineComponents b ; for( unsigned int d=0 ; d<=Degree ; d++ ) b[d] = polys[d].scale(s) ; return b; }
    BSplineComponents shift( double s ) const { BSplineComponents b ; for( unsigned int d=0 ; d<=Degree ; d++ ) b[d] = polys[d].shift(s) ; return b; }
  };
  const static int  VV_DOT_FLAG = 1;
  const static int  DV_DOT_FLAG = 2;
  const static int  DD_DOT_FLAG = 4;
  const static int   VALUE_FLAG = 1;
  const static int D_VALUE_FLAG = 2;

  int depth , functionCount , sampleCount;
  Pointer( Real ) vvDotTable;
  Pointer( Real ) dvDotTable;
  Pointer( Real ) ddDotTable;
  Pointer( Real ) valueTables;
  Pointer( Real ) dValueTables;
  PPolynomial< Degree   >  baseFunction ,  leftBaseFunction ,  rightBaseFunction ,  leftRightBaseFunction;
  PPolynomial< Degree-1 > dBaseFunction , dLeftBaseFunction , dRightBaseFunction , dLeftRightBaseFunction;
  BSplineComponents baseBSpline , leftBSpline , rightBSpline , leftRightBSpline;
  Pointer( PPolynomial< Degree > ) baseFunctions;
  Pointer( BSplineComponents ) baseBSplines;

  BSplineData(void);
  ~BSplineData(void);

  virtual void   setDotTables( int flags , bool inset=false );
  virtual void clearDotTables( int flags );

  virtual void   setValueTables( int flags , double smooth=0 );
  virtual void   setValueTables( int flags , double valueSmooth , double normalSmooth );
  virtual void clearValueTables( void );

  void setSampleSpan( int idx , int& start , int& end , double smooth=0 ) const;

  /********************************************************
   * Sets the translates and scales of the basis function
   * up to the prescribed depth
   * <maxDepth> the maximum depth
   * <useDotRatios> specifies if dot-products of derivatives
   * should be pre-divided by function integrals
   * <reflectBoundary> spcifies if function space should be
   * forced to be reflectively symmetric across the boundary
   ********************************************************/
  void set( int maxDepth , bool useDotRatios=true , int boundaryType=BSplineElements< Degree >::NONE );

  inline int Index( int i1 , int i2 ) const;
  static inline int SymmetricIndex( int i1 , int i2 );
  static inline int SymmetricIndex( int i1 , int i2 , int& index  );
136
137
138
139
140
141
};

template< int Degree1 , int Degree2 > void SetBSplineElementIntegrals( double integrals[Degree1+1][Degree2+1] );

#include "BSplineData.inl"
#endif // BSPLINE_DATA_INCLUDED