Small OpenGL based c++ rendering library
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

catmullromspline.cpp 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /* libsegl - Sebas Extended GL Library
  2. * Collection of Opengl/3D-Math helpers
  3. *
  4. * Copyright (c) 2008 by Sebastian Lohff, seba@seba-geek.de
  5. * http://www.seba-geek.de
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Library General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Library General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Library General Public
  18. * License along with this library; if not, write to the
  19. * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
  20. * Boston, MA 02110-1301, USA.
  21. */
  22. #include "catmullromspline.h"
  23. namespace segl {
  24. CatmullRomSpline::CatmullRomSpline(float _s) {
  25. cr = new Matrix(4, 4);
  26. setS(_s);
  27. }
  28. void CatmullRomSpline::makeMatrix() {
  29. cr->set(-s , 0, 0);
  30. cr->set( 2-s , 0, 1);
  31. cr->set( s-2 , 0, 2);
  32. cr->set( s , 0, 3);
  33. cr->set( 2*s , 1, 0);
  34. cr->set( s-3 , 1, 1);
  35. cr->set( 3-2*s, 1, 2);
  36. cr->set(-s , 1, 3);
  37. cr->set(-s, 2, 0);
  38. cr->set( 0, 2, 1);
  39. cr->set( s, 2, 2);
  40. cr->set( 0, 2, 3);
  41. cr->set( 0, 3, 0);
  42. cr->set( 1, 3, 1);
  43. cr->set( 0, 3, 2);
  44. cr->set( 0, 3, 3);
  45. }
  46. Matrix CatmullRomSpline::getPosition(float u, Matrix &controlpoints) {
  47. // Matrix controlpoints(4, a.getN());
  48. // for(int i=0; i<a.getN(); i++) {
  49. // controlpoints.set(a.get(0, i), 0, i);
  50. // controlpoints.set(b.get(0, i), 1, i);
  51. // controlpoints.set(c.get(0, i), 2, i);
  52. // controlpoints.set(d.get(0, i), 3, i);
  53. // }
  54. Matrix param(1, 4);
  55. param.set(u*u*u, 0, 0);
  56. param.set(u*u, 0, 1);
  57. param.set(u, 0, 2);
  58. param.set(1, 0, 3);
  59. return param * (*cr) * controlpoints;
  60. }
  61. Punkt3D CatmullRomSpline::getPosition(float u, Punkt3D a, Punkt3D b, Punkt3D c, Punkt3D d) {
  62. Matrix erg(1, 3);
  63. Matrix controlpoints(4, 3);
  64. controlpoints.set(a.x, 0, 0);
  65. controlpoints.set(a.y, 0, 1);
  66. controlpoints.set(a.z, 0, 2);
  67. controlpoints.set(b.x, 1, 0);
  68. controlpoints.set(b.y, 1, 1);
  69. controlpoints.set(b.z, 1, 2);
  70. controlpoints.set(c.x, 2, 0);
  71. controlpoints.set(c.y, 2, 1);
  72. controlpoints.set(c.z, 2, 2);
  73. controlpoints.set(d.x, 3, 0);
  74. controlpoints.set(d.y, 3, 1);
  75. controlpoints.set(d.z, 3, 2);
  76. erg = getPosition(u, controlpoints);
  77. return Punkt3D(erg.get(0,0), erg.get(0,1), erg.get(0,2));
  78. }
  79. Punkt2D CatmullRomSpline::getPosition(float u, Punkt2D a, Punkt2D b, Punkt2D c, Punkt2D d) {
  80. Matrix erg(1, 2);
  81. Matrix controlpoints(4, 2);
  82. controlpoints.set(a.x, 0, 0);
  83. controlpoints.set(a.y, 0, 1);
  84. controlpoints.set(b.x, 1, 0);
  85. controlpoints.set(b.y, 1, 1);
  86. controlpoints.set(c.x, 2, 0);
  87. controlpoints.set(c.y, 2, 1);
  88. controlpoints.set(d.x, 3, 0);
  89. controlpoints.set(d.y, 3, 1);
  90. erg = getPosition(u, controlpoints);
  91. return Punkt2D(erg.get(0,0), erg.get(0,1));
  92. }
  93. void CatmullRomSpline::setS(float _s) {
  94. s = _s;
  95. makeMatrix();
  96. }
  97. CatmullRomSpline::~CatmullRomSpline() {
  98. delete(cr);
  99. }
  100. } // namespace segl