cylinder.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * DoRayMe - a quick and dirty Raytracer
  3. * Cylinder implementation
  4. *
  5. * Created by Manoël Trapier
  6. * Copyright (c) 2020 986-Studio.
  7. *
  8. */
  9. #include <tuple.h>
  10. #include <ray.h>
  11. #include <shape.h>
  12. #include <cylinder.h>
  13. #include <math_helper.h>
  14. bool Cylinder::checkCap(Ray r, double t)
  15. {
  16. /* Helping function to reduce duplication.
  17. * Checks to see if the intersection ot t is within a radius
  18. * of 1 (the radius of our cylinder from the y axis
  19. */
  20. double x = r.origin.x + t * r.direction.x;
  21. double z = r.origin.z + t * r.direction.z;
  22. return (x * x + z * z) <= 1;
  23. }
  24. void Cylinder::intersectCaps(Ray r, Intersect &xs)
  25. {
  26. /* Caps only mattter is the cylinder is closed, and might possibly be
  27. * intersected by the ray
  28. */
  29. if ((this->isClosed) && (fabs(r.direction.y) > getEpsilon()))
  30. {
  31. double t;
  32. /* Check for an intersection with the lower end cap by intersecting
  33. * the ray with the plan at y = this->minCap
  34. */
  35. t = (this->minCap - r.origin.y) / r.direction.y;
  36. if (this->checkCap(r, t))
  37. {
  38. xs.add(Intersection(t, this));
  39. }
  40. /* Check for an intersection with the upper end cap by intersecting
  41. * the ray with the plan at y = this->maxCap
  42. */
  43. t = (this->maxCap - r.origin.y) / r.direction.y;
  44. if (this->checkCap(r, t))
  45. {
  46. xs.add(Intersection(t, this));
  47. }
  48. }
  49. }
  50. Intersect Cylinder::localIntersect(Ray r)
  51. {
  52. Intersect ret;
  53. double A = r.direction.x * r.direction.x + r.direction.z * r.direction.z;
  54. /* Ray is parallel to the Y axis */
  55. if (A >= getEpsilon())
  56. {
  57. double B = 2 * r.origin.x * r.direction.x +
  58. 2 * r.origin.z * r.direction.z;
  59. double C = r.origin.x * r.origin.x + r.origin.z * r.origin.z - 1;
  60. double disc = B * B - 4 * A * C;
  61. if (disc >= 0)
  62. {
  63. double t0 = (-B - sqrt(disc)) / (2 * A);
  64. double t1 = (-B + sqrt(disc)) / (2 * A);
  65. double y0 = r.origin.y + t0 * r.direction.y;
  66. if ((this->minCap < y0) && (y0 < this->maxCap))
  67. {
  68. ret.add(Intersection(t0, this));
  69. }
  70. double y1 = r.origin.y + t1 * r.direction.y;
  71. if ((this->minCap < y1) && (y1 < this->maxCap))
  72. {
  73. ret.add(Intersection(t1, this));
  74. }
  75. }
  76. }
  77. this->intersectCaps(r, ret);
  78. return ret;
  79. }
  80. Tuple Cylinder::localNormalAt(Tuple point, Intersection *hit)
  81. {
  82. /* Compute the square of the distance from the Y axis */
  83. double dist = point.x * point.x + point.z * point.z;
  84. if ((dist < 1) && (point.y >= (this->maxCap - getEpsilon())))
  85. {
  86. return Vector(0, 1, 0);
  87. }
  88. if ((dist < 1) && (point.y <= this->minCap + getEpsilon()))
  89. {
  90. return Vector(0, -1, 0);
  91. }
  92. return Vector(point.x, 0, point.z);
  93. }
  94. BoundingBox Cylinder::getLocalBounds()
  95. {
  96. BoundingBox ret;
  97. ret | Point(-1, this->minCap, -1);
  98. ret | Point(1, this->maxCap, 1);
  99. return ret;
  100. }
  101. void Cylinder::dumpMe(FILE *fp)
  102. {
  103. fprintf(fp, "\"Type\": \"Cylinder\",\n");
  104. Tuple t = this->transformMatrix * Point(0, 0, 0);
  105. fprintf(fp, "\"pseudocenter\": { \"x\": %f, \"y\": %f, \"z\": %f}, \n",
  106. t.x, t.y, t.z);
  107. t = this->transformMatrix * Point(0, this->minCap, 0);
  108. fprintf(fp, "\"min\": %f, \n", t.y);
  109. t = this->transformMatrix * Point(1, this->maxCap, 1);
  110. fprintf(fp, "\"max\": %f, \n", t.y);
  111. Shape::dumpMe(fp);
  112. }