mbsim  4.0.0
MBSim Kernel
nurbs_defs.h
1#ifndef NURBS_DEFS_H_
2#define NURBS_DEFS_H_
3
4#include <fmatvec/fmatvec.h>
5#include <mbsim/mbsim_event.h>
6#include <iostream>
7
8namespace fmatvec {
12 template <int N>
13 class Point : public Vector<Fixed<N>, double> {
14 public:
15 Point();
16
17 Point(Vector<Fixed<N>, double> pnt);
18
19 virtual ~Point();
20
21 protected:
22 };
23
24 template <int N>
26 Vector<Fixed<N>, double>() {
27
28 }
29
30 template <int N>
31 Point<N>::Point(Vector<Fixed<N>, double> pnt) :
32 Vector<Fixed<N>, double>(pnt) {
33 }
34
35 template <int N>
36 Point<N>::~Point() = default;
37
41 template <int N>
42 class HPoint : public Vector<Fixed<N + 1>, double> {
43 public:
44 HPoint();
45
46 HPoint(Vector<Fixed<N + 1>, double> pnt);
47
48 virtual ~HPoint();
49
50 Point<N> projectW();
51
52 double w();
53
54 double w() const;
55
56 protected:
57 };
58
59 template <int N>
61 Vector<Fixed<N + 1>, double>() {
62 }
63
64 template <int N>
65 HPoint<N>::HPoint(Vector<Fixed<N + 1>, double> pnt) :
66 Vector<Fixed<N + 1>, double>(pnt) {
67 }
68
69 template <int N>
70 HPoint<N>::~HPoint() = default;
71
72 template <int N>
73 Point<N> HPoint<N>::projectW() {
74 Point<N> pnt;
75 for (int i = 0; i < N; i++)
76 pnt(i) = (*this)(i);
77 return pnt;
78 }
79
80 template <int N>
81 double HPoint<N>::w() {
82 return (*this)(N);
83 }
84
85 template <int N>
86 double HPoint<N>::w() const {
87 return (*this)(N);
88 }
89
90 /*mathematical operations*/
91 template <int N>
92 Point<N> project(const HPoint<N> & hPnt) {
93 Point<N> pnt;
94 for (int i = 0; i < N; i++)
95 pnt(i) = hPnt(i) / hPnt.w();
96 return pnt;
97 }
98// /*!
99// * \brief wrapper class for nurbs surface interpolating data point
100// */
101
102 template <typename T>
103 class GeneralMatrix {
104 public:
105 GeneralMatrix();
106 GeneralMatrix(int nrows_, int ncols_);
107 GeneralMatrix(const GeneralMatrix<T>& m);
108 ~GeneralMatrix();
109
110 GeneralMatrix<T>& operator=(const GeneralMatrix<T>& m);
111
112 T& operator()(int row, int col);
113 T const& operator()(int row, int col) const;
114 int rows() const;
115 int cols() const;
116 void resize(int nrows_, int ncols_);
117 template <typename J>
118 friend std::ostream& operator<<(std::ostream &os, const GeneralMatrix<J> &A);
119 private:
120 int nrows{0}, ncols{0};
121 T* data;
122 };
123
124 template <typename T>
125 inline GeneralMatrix<T>::GeneralMatrix() :
126 data(nullptr) {
127 }
128
129 template <typename T>
130 inline GeneralMatrix<T>::GeneralMatrix(int nrows_, int ncols_) :
131 nrows(nrows_), ncols(ncols_) {
132 if (nrows_ == 0 || ncols_ == 0)
133 throw std::runtime_error("(GeneralMatrix:: The number of rows or columns of the matrix is zero!)");
134 data = new T[nrows_ * ncols_];
135 }
136 template <typename T>
137 inline GeneralMatrix<T>::GeneralMatrix(const GeneralMatrix<T>& m) :
138 nrows(m.nrows), ncols(m.ncols) {
139 data = new T[nrows * ncols];
140 std::copy(m.data, m.data + nrows * ncols, data);
141 }
142
143 template <typename T>
144 inline GeneralMatrix<T>::~GeneralMatrix() {
145 delete[] data;
146 }
147
148 template <typename T>
149 inline GeneralMatrix<T>& GeneralMatrix<T>::operator=(const GeneralMatrix<T>& m) {
150 nrows = m.nrows;
151 ncols = m.ncols;
152 delete[] data;
153 data = new T[nrows * ncols];
154 std::copy(m.data, m.data + nrows * ncols, data);
155 return *this;
156 }
157
158 template <typename T>
159 inline T& GeneralMatrix<T>::operator()(int row, int col) {
160 if (row >= nrows || col >= ncols)
161 throw std::runtime_error("(GeneralMatrix::operator(): trying to access data out of range of the GeneralMatrix!)");
162 return data[row * ncols + col];
163 }
164
165 template <typename T>
166 inline T const& GeneralMatrix<T>::operator()(int row, int col) const {
167 if (row >= nrows || col >= ncols)
168 throw std::runtime_error("(GeneralMatrix::operator(): trying to access data out of range of the GeneralMatrix!)");
169 return data[row * ncols + col];
170 }
171
172 template <typename T>
173 inline int GeneralMatrix<T>::rows() const {
174 return nrows;
175 }
176
177 template <typename T>
178 inline int GeneralMatrix<T>::cols() const {
179 return ncols;
180 }
181
182 template <typename T>
183 void GeneralMatrix<T>::resize(int nrows_, int ncols_) {
184
185 if (nrows_ <= 0 || ncols_ <= 0) {
186 throw std::runtime_error("(GeneralMatrix::resize(): non-positive resize number of rows or columns!)");
187 return;
188 }
189
190 if (nrows_ == nrows && ncols_ == ncols)
191 return;
192
193 auto* newData = new T[nrows_ * ncols_];
194
195 delete[] data;
196 data = newData;
197 nrows = nrows_;
198 ncols = ncols_;
199 }
200
201 template <typename T>
202 std::ostream& operator<<(std::ostream &os, const GeneralMatrix<T> &A) {
203 os << A.rows() << " x " << A.cols() << std::endl;
204 os << " = " << std::endl;
205 os << "[ ";
206 for (int i = 0; i < A.rows(); ++i) {
207 for (int j = 0; j < A.cols(); ++j) {
208 os << trans(A.data[i * A.ncols + j]);
209 }
210 if (i != A.rows() - 1)
211 os << std::endl << " ";
212 }
213 os << " ];";
214 return os;
215 }
216
217}
218
219#endif
wrapper class for nurbs type HPoint
Definition: nurbs_defs.h:42
wrapper class for nurbs type Point
Definition: nurbs_defs.h:13