cone.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * DoRayMe - a quick and dirty Raytracer
  3. * Cone 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 <cone.h>
  13. #include <math_helper.h>
  14. bool Cone::checkCap(Ray r, double t, double y)
  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 Cone 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) <= fabs(y);
  23. }
  24. void Cone::intersectCaps(Ray r, Intersect &xs)
  25. {
  26. /* Caps only mattter is the Cone 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, this->minCap))
  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, this->maxCap))
  45. {
  46. xs.add(Intersection(t, this));
  47. }
  48. }
  49. }
  50. void Cone::localIntersect(Ray r, Intersect &xs)
  51. {
  52. double A = (r.direction.x * r.direction.x) -
  53. (r.direction.y * r.direction.y) +
  54. (r.direction.z * r.direction.z);
  55. double B = (2 * r.origin.x * r.direction.x) -
  56. (2 * r.origin.y * r.direction.y) +
  57. (2 * r.origin.z * r.direction.z);
  58. double C = (r.origin.x * r.origin.x) -
  59. (r.origin.y * r.origin.y) +
  60. (r.origin.z * r.origin.z);
  61. if ((fabs(A) <= getEpsilon()) && (fabs(B) >= getEpsilon()))
  62. {
  63. double t = -C / (2*B);
  64. xs.add(Intersection(t, this));
  65. }
  66. else if (fabs(A) >= getEpsilon())
  67. {
  68. double disc = (B * B) - 4 * A * C;
  69. if (disc >= 0)
  70. {
  71. double t0 = (-B - sqrt(disc)) / (2 * A);
  72. double t1 = (-B + sqrt(disc)) / (2 * A);
  73. double y0 = r.origin.y + t0 * r.direction.y;
  74. if ((this->minCap < y0) && (y0 < this->maxCap))
  75. {
  76. xs.add(Intersection(t0, this));
  77. }
  78. double y1 = r.origin.y + t1 * r.direction.y;
  79. if ((this->minCap < y1) && (y1 < this->maxCap))
  80. {
  81. xs.add(Intersection(t1, this));
  82. }
  83. }
  84. }
  85. this->intersectCaps(r, xs);
  86. }
  87. Tuple Cone::localNormalAt(Tuple point, Intersection *hit)
  88. {
  89. /* Compute the square of the distance from the Y axis */
  90. double dist = point.x * point.x + point.z * point.z;
  91. if ((dist < 1) && (point.y >= (this->maxCap - getEpsilon())))
  92. {
  93. return Vector(0, 1, 0);
  94. }
  95. if ((dist < 1) && (point.y <= this->minCap + getEpsilon()))
  96. {
  97. return Vector(0, -1, 0);
  98. }
  99. double y = sqrt(point.x * point.x + point.z * point.z);
  100. if (point.y > 0)
  101. {
  102. y = -y;
  103. }
  104. return Vector(point.x, y, point.z);
  105. }
  106. BoundingBox Cone::getLocalBounds()
  107. {
  108. BoundingBox ret;
  109. double a = fabs(this->minCap);
  110. double b = fabs(this->maxCap);
  111. double limit = (a > b)?a:b;
  112. ret | Point(-limit, this->minCap, -limit);
  113. ret | Point(limit, this->maxCap, limit);
  114. return ret;
  115. }
  116. void Cone::dumpMe(FILE *fp)
  117. {
  118. fprintf(fp, "\"Type\": \"Cylinder\",\n");
  119. Tuple t = this->transformMatrix * Point(0, 0, 0);
  120. fprintf(fp, "\"pseudocenter\": { \"x\": %f, \"y\": %f, \"z\": %f}, \n",
  121. t.x, t.y, t.z);
  122. t = this->transformMatrix * Point(0, this->minCap, 0);
  123. fprintf(fp, "\"min\": %f, \n", t.y);
  124. t = this->transformMatrix * Point(1, this->maxCap, 1);
  125. fprintf(fp, "\"max\": %f, \n", t.y);
  126. Shape::dumpMe(fp);
  127. }