9 #ifndef TINYSPLINECXX_API
10 #define TINYSPLINECXX_API TINYSPLINE_API
25 #ifdef TINYSPLINE_EMSCRIPTEN
29 #include <emscripten/bind.h>
30 using namespace emscripten;
35 {
throw std::runtime_error(
"cannot write read-only property"); }
39 exceptionMessage(
int ptr)
40 {
return std::string(
reinterpret_cast<std::exception *
>(ptr)->what()); }
44 namespace emscripten {
46 template <
typename T,
typename Allocator>
47 struct BindingType<std::vector<T, Allocator>> {
48 using ValBinding = BindingType<val>;
49 using WireType = ValBinding::WireType;
52 toWireType(
const std::vector<T, Allocator> &vec)
53 {
return ValBinding::toWireType(val::array(vec)); }
55 static std::vector<T, Allocator>
56 fromWireType(WireType value)
57 {
return vecFromJSArray<T>(ValBinding::fromWireType(value)); }
62 typename std::enable_if_t<std::is_same<
63 typename Canonicalized<T>::type,
64 std::vector<typename Canonicalized<T>::type::value_type,
65 typename Canonicalized<T>::type::allocator_type>>::value>> {
66 static constexpr TYPEID
67 get() {
return TypeID<val>::get(); }
76 namespace tinyspline {
97 using std_real_vector_in = std::vector<tinyspline::real> *;
98 using std_real_vector_out = std::vector<tinyspline::real> *;
100 using std_real_vector_in =
const std::vector<tinyspline::real> &;
101 using std_real_vector_out = std::vector<tinyspline::real>;
116 Vec2(real x, real y);
120 Vec2 operator*(real scalar);
126 std::vector<real> values()
const;
129 Vec2 subtract(
const Vec2 &other)
const;
130 Vec2 multiply(real scalar)
const;
131 Vec2 normalize()
const;
132 real magnitude()
const;
133 real dot(
const Vec2 &other)
const;
134 real angle(
const Vec2 &other)
const;
135 real distance(
const Vec2 &other)
const;
137 std::string toString()
const;
142 #ifdef TINYSPLINE_EMSCRIPTEN
144 void setValues(std::vector<real>) { cannotWrite(); }
151 Vec3(real x, real y, real z);
155 Vec3 operator*(real scalar);
163 std::vector<real> values()
const;
166 Vec3 subtract(
const Vec3 &other)
const;
167 Vec3 multiply(real scalar)
const;
168 Vec3 cross(
const Vec3 &other)
const;
169 Vec3 normalize()
const;
170 real magnitude()
const;
171 real dot(
const Vec3 &other)
const;
172 real angle(
const Vec3 &other)
const;
173 real distance(
const Vec3 &other)
const;
175 std::string toString()
const;
180 #ifdef TINYSPLINE_EMSCRIPTEN
182 void setValues(std::vector<real>) { cannotWrite(); }
189 Vec4(real x, real y, real z, real w);
193 Vec4 operator*(real scalar);
203 std::vector<real> values()
const;
206 Vec4 subtract(
const Vec4 &other)
const;
207 Vec4 multiply(real scalar)
const;
208 Vec4 normalize()
const;
209 real magnitude()
const;
210 real dot(
const Vec4 &other)
const;
211 real angle(
const Vec4 &other)
const;
212 real distance(
const Vec4 &other)
const;
214 std::string toString()
const;
219 #ifdef TINYSPLINE_EMSCRIPTEN
221 void setValues(std::vector<real>) { cannotWrite(); }
225 #ifdef TINYSPLINE_EMSCRIPTEN
237 add3(
const Vec3 &a,
const Vec3 &b)
241 add4(
const Vec4 &a,
const Vec4 &b)
250 subtract2(
const Vec2 &a,
const Vec2 &b)
251 {
return a.subtract(b); }
254 subtract3(
const Vec3 &a,
const Vec3 &b)
255 {
return a.subtract(b); }
258 subtract4(
const Vec4 &a,
const Vec4 &b)
259 {
return a.subtract(b); }
267 multiply2(
const Vec2 &vec, real scalar)
268 {
return vec.multiply(scalar); }
271 multiply3(
const Vec3 &vec, real scalar)
272 {
return vec.multiply(scalar); }
275 multiply4(
const Vec4 &vec, real scalar)
276 {
return vec.multiply(scalar); }
284 cross3(
const Vec3 &a, Vec3 &b)
285 {
return a.cross(b); }
293 normalize2(
const Vec2 &vec)
294 {
return vec.normalize(); }
297 normalize3(
const Vec3 &vec)
298 {
return vec.normalize(); }
301 normalize4(
const Vec4 &vec)
302 {
return vec.normalize(); }
310 magnitude2(
const Vec2 &vec)
311 {
return vec.magnitude(); }
314 magnitude3(
const Vec3 &vec)
315 {
return vec.magnitude(); }
318 magnitude4(
const Vec4 &vec)
319 {
return vec.magnitude(); }
327 dot2(
const Vec2 &a, Vec2 &b)
331 dot3(
const Vec3 &a, Vec3 &b)
335 dot4(
const Vec4 &a, Vec4 &b)
344 angle2(
const Vec2 &a, Vec2 &b)
345 {
return a.angle(b); }
348 angle3(
const Vec3 &a, Vec3 &b)
349 {
return a.angle(b); }
352 angle4(
const Vec4 &a, Vec4 &b)
353 {
return a.angle(b); }
361 distance2(
const Vec2 &a, Vec2 &b)
362 {
return a.distance(b); }
365 distance3(
const Vec3 &a, Vec3 &b)
366 {
return a.distance(b); }
369 distance4(
const Vec4 &a, Vec4 &b)
370 {
return a.distance(b); }
392 Vec3 position()
const;
393 Vec3 tangent()
const;
395 Vec3 binormal()
const;
397 std::string toString()
const;
405 #ifdef TINYSPLINE_EMSCRIPTEN
408 void setPosition(
Vec3) { cannotWrite(); }
409 void setTangent(
Vec3) { cannotWrite(); }
410 void setNormal(
Vec3) { cannotWrite(); }
411 void setBinormal(
Vec3) { cannotWrite(); }
426 Frame at(
size_t idx)
const;
428 std::string toString()
const;
451 Domain(real min, real max);
456 std::string toString()
const;
462 #ifdef TINYSPLINE_EMSCRIPTEN
467 void setMin(real) { cannotWrite(); }
468 void setMax(real) { cannotWrite(); }
491 size_t index()
const;
492 size_t multiplicity()
const;
493 size_t numInsertions()
const;
494 size_t dimension()
const;
495 std::vector<real> points()
const;
496 std::vector<real> result()
const;
511 Vec2 resultVec2(
size_t idx = 0)
const;
526 Vec3 resultVec3(
size_t idx = 0)
const;
541 Vec4 resultVec4(
size_t idx = 0)
const;
543 std::string toString()
const;
553 #ifdef TINYSPLINE_EMSCRIPTEN
556 void setKnot(real) { cannotWrite(); }
557 void setIndex(
size_t) { cannotWrite(); }
558 void setMultiplicity(
size_t) { cannotWrite(); }
559 void setNumInsertions(
size_t) { cannotWrite(); }
560 void setDimension(
size_t) { cannotWrite(); }
561 void setPoints(std::vector<real>) { cannotWrite(); }
562 void setResult(std::vector<real>) { cannotWrite(); }
579 enum Type { Opened, Clamped, Beziers };
585 explicit BSpline(
size_t numControlPoints,
586 size_t dimension = 2,
588 Type type = Type::Clamped);
592 static BSpline interpolateCubicNatural(std_real_vector_in points,
594 static BSpline interpolateCatmullRom(std_real_vector_in points,
596 real alpha = (real) 0.5,
597 std::vector<real> *first =
nullptr,
598 std::vector<real> *last =
nullptr,
600 static BSpline parseJson(std::string json);
601 static BSpline load(std::string path);
603 static bool knotsEqual(real x, real y);
611 size_t degree()
const;
612 size_t order()
const;
613 size_t dimension()
const;
614 std::vector<real> controlPoints()
const;
615 Vec2 controlPointVec2At(
size_t idx)
const;
616 Vec3 controlPointVec3At(
size_t idx)
const;
617 Vec4 controlPointVec4At(
size_t idx)
const;
618 std::vector<real> knots()
const;
619 real knotAt(
size_t idx)
const;
622 size_t numControlPoints()
const;
624 std_real_vector_out evalAll(std_real_vector_in knots)
const;
625 std_real_vector_out sample(
size_t num = 0)
const;
627 real epsilon = (real) 0.0,
628 bool persnickety =
false,
630 bool ascending =
true,
631 size_t maxIter = 50)
const;
634 FrameSeq computeRMF(std_real_vector_in knots,
635 Vec3 *firstNormal =
nullptr)
const;
636 BSpline subSpline(real knot0, real knot1)
const;
637 std_real_vector_out uniformKnotSeq(
size_t num = 100)
const;
638 std_real_vector_out equidistantKnotSeq(
size_t num = 100,
639 size_t numSamples = 0)
const;
640 ChordLengths chordLengths(std_real_vector_in knots)
const;
641 ChordLengths chordLengths(
size_t numSamples = 200)
const;
644 std::string toJson()
const;
645 void save(std::string path)
const;
648 void setControlPoints(
const std::vector<real> &ctrlp);
649 void setControlPointVec2At(
size_t idx,
Vec2 &cp);
650 void setControlPointVec3At(
size_t idx,
Vec3 &cp);
651 void setControlPointVec4At(
size_t idx,
Vec4 &cp);
652 void setKnots(
const std::vector<real> &knots);
653 void setKnotAt(
size_t idx, real knot);
656 BSpline insertKnot(real knot,
size_t num)
const;
657 BSpline split(real knot)
const;
658 BSpline tension(real beta)
const;
662 BSpline elevateDegree(
size_t amount,
671 std::string toString()
const;
682 #ifdef TINYSPLINE_EMSCRIPTEN
684 std_real_vector_out sample0()
const {
return sample(); }
685 std_real_vector_out sample1(
size_t num)
const {
return sample(num); }
686 BSpline derive0()
const {
return derive(); }
687 BSpline derive1(
size_t n)
const {
return derive(n); }
688 BSpline derive2(
size_t n, real eps)
const {
return derive(n, eps); }
707 real epsilon()
const;
712 std::string toString()
const;
716 BSpline m_originAligned, m_targetAligned;
737 std::vector<real> knots()
const;
738 std::vector<real> lengths()
const;
739 TS_DEPRECATED std::vector<real> values()
const;
742 real arcLength()
const;
743 real lengthToKnot(real len)
const;
744 real tToKnot(real t)
const;
745 std_real_vector_out equidistantKnotSeq(
size_t num = 100)
const;
747 std::string toString()
const;
750 real *m_knots, *m_lengths;
764 #ifdef TINYSPLINE_EMSCRIPTEN
765 using namespace tinyspline;
766 EMSCRIPTEN_BINDINGS(tinyspline) {
767 function(
"exceptionMessage", &exceptionMessage);
770 value_object<Vec2>(
"Vec2")
781 value_object<Vec3>(
"Vec3")
795 value_object<Vec4>(
"Vec4")
812 class_<VecMath>(
"VecMath")
813 .class_function(
"add", &VecMath::add2)
814 .class_function(
"add", &VecMath::add3)
815 .class_function(
"add", &VecMath::add4)
816 .class_function(
"subtract", &VecMath::subtract2)
817 .class_function(
"subtract", &VecMath::subtract3)
818 .class_function(
"subtract", &VecMath::subtract4)
819 .class_function(
"multiply", &VecMath::multiply2)
820 .class_function(
"multiply", &VecMath::multiply3)
821 .class_function(
"multiply", &VecMath::multiply4)
822 .class_function(
"cross", &VecMath::cross3)
823 .class_function(
"normalize", &VecMath::normalize2)
824 .class_function(
"normalize", &VecMath::normalize3)
825 .class_function(
"normalize", &VecMath::normalize4)
826 .class_function(
"magnitude", &VecMath::magnitude2)
827 .class_function(
"magnitude", &VecMath::magnitude3)
828 .class_function(
"magnitude", &VecMath::magnitude4)
829 .class_function(
"dot", &VecMath::dot2)
830 .class_function(
"dot", &VecMath::dot3)
831 .class_function(
"dot", &VecMath::dot4)
832 .class_function(
"angle", &VecMath::angle2)
833 .class_function(
"angle", &VecMath::angle3)
834 .class_function(
"angle", &VecMath::angle4)
835 .class_function(
"distance", &VecMath::distance2)
836 .class_function(
"distance", &VecMath::distance3)
837 .class_function(
"distance", &VecMath::distance4)
841 value_object<Frame>(
"Frame")
855 class_<FrameSeq>(
"FrameSeq")
857 .constructor<FrameSeq>()
858 .function(
"size", &FrameSeq::size)
859 .function(
"at", &FrameSeq::at)
860 .function(
"toString", &FrameSeq::toString)
864 value_object<Domain>(
"Domain")
873 value_object<DeBoorNet>(
"DeBoorNet")
879 &DeBoorNet::setIndex)
880 .field(
"multiplicity",
881 &DeBoorNet::multiplicity,
882 &DeBoorNet::setMultiplicity)
883 .field(
"numInsertions",
884 &DeBoorNet::numInsertions,
885 &DeBoorNet::setNumInsertions)
887 &DeBoorNet::dimension,
888 &DeBoorNet::setDimension)
891 &DeBoorNet::setPoints)
894 &DeBoorNet::setResult)
897 class_<BSpline>(
"BSpline")
900 .constructor<size_t>()
901 .constructor<size_t,
size_t>()
902 .constructor<size_t, size_t, size_t>()
903 .constructor<size_t, size_t, size_t, BSpline::Type>()
905 .class_function(
"interpolateCubicNatural",
906 &BSpline::interpolateCubicNatural)
907 .class_function(
"interpolateCatmullRom",
908 &BSpline::interpolateCatmullRom,
909 allow_raw_pointers())
910 .class_function(
"parseJson", &BSpline::parseJson)
912 .property(
"degree", &BSpline::degree)
913 .property(
"order", &BSpline::order)
914 .property(
"dimension", &BSpline::dimension)
915 .property(
"controlPoints", &BSpline::controlPoints,
916 &BSpline::setControlPoints)
917 .property(
"knots", &BSpline::knots, &BSpline::setKnots)
918 .property(
"domain", &BSpline::domain)
921 .function(
"knotAt", &BSpline::knotAt)
922 .function(
"setKnotAt", &BSpline::setKnotAt)
925 .function(
"numControlPoints", &BSpline::numControlPoints)
926 .function(
"eval", &BSpline::eval)
927 .function(
"evalAll", &BSpline::evalAll)
929 select_overload<std_real_vector_out()
const>
932 select_overload<std_real_vector_out(
size_t)
const>
934 .function(
"sample", &BSpline::sample)
935 .function(
"bisect", &BSpline::bisect)
936 .function(
"isClosed", &BSpline::isClosed)
939 .function(
"toJson", &BSpline::toJson)
942 .function(
"insertKnot", &BSpline::insertKnot)
943 .function(
"split", &BSpline::split)
944 .function(
"tension", &BSpline::tension)
945 .function(
"toBeziers", &BSpline::toBeziers)
947 select_overload<
BSpline()
const>
950 select_overload<
BSpline(
size_t)
const>
953 select_overload<
BSpline(
size_t, real)
const>
957 .function(
"toString", &BSpline::toString)
960 enum_<BSpline::Type>(
"BSplineType")
961 .value(
"Opened", BSpline::Type::Opened)
962 .value(
"Clamped", BSpline::Type::Clamped)
963 .value(
"Beziers", BSpline::Type::Beziers)
Definition: tinysplinecxx.h:577
Definition: tinysplinecxx.h:726
Definition: tinysplinecxx.h:481
Definition: tinysplinecxx.h:449
Definition: tinysplinecxx.h:415
Definition: tinysplinecxx.h:385
Definition: tinysplinecxx.h:699
Definition: tinysplinecxx.h:113
Definition: tinysplinecxx.h:148
Definition: tinysplinecxx.h:186
Definition: tinyspline.h:718
Definition: tinyspline.h:1308
Definition: tinyspline.h:665
#define TS_DOMAIN_DEFAULT_MAX
Definition: tinyspline.h:135
#define TS_DOMAIN_DEFAULT_MIN
Definition: tinyspline.h:128
double tsReal
Definition: tinyspline.h:213
#define TS_POINT_EPSILON
Definition: tinyspline.h:169
tsDeBoorNet TINYSPLINE_API ts_deboornet_init(void)
Definition: tinyspline.c:693