cone.cpp 4.0 KB

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