joonsrenderer 1.1-java

Sign up to get free protection for your applications and to get access to all the features.
Files changed (255) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +53 -0
  3. data/.mvn/extensions.xml +8 -0
  4. data/CHANGELOG.md +2 -0
  5. data/Gemfile +6 -0
  6. data/LICENSE +674 -0
  7. data/README.md +2 -0
  8. data/Rakefile +43 -0
  9. data/docs/.gitignore +6 -0
  10. data/docs/_config.yml +20 -0
  11. data/docs/_includes/footer.html +38 -0
  12. data/docs/_includes/head.html +15 -0
  13. data/docs/_includes/header.html +27 -0
  14. data/docs/_includes/icon-github.html +1 -0
  15. data/docs/_includes/icon-github.svg +1 -0
  16. data/docs/_includes/icon-twitter.html +1 -0
  17. data/docs/_includes/icon-twitter.svg +1 -0
  18. data/docs/_layouts/default.html +20 -0
  19. data/docs/_layouts/page.html +14 -0
  20. data/docs/_layouts/post.html +15 -0
  21. data/docs/_posts/2017-01-08-animated_ray_tracing.md +72 -0
  22. data/docs/_posts/2017-01-08-welcome.md +78 -0
  23. data/docs/_sass/_base.scss +206 -0
  24. data/docs/_sass/_layout.scss +242 -0
  25. data/docs/_sass/_syntax-highlighting.scss +71 -0
  26. data/docs/about.md +12 -0
  27. data/docs/assets/Animation.ogv +0 -0
  28. data/docs/assets/Animation.png +0 -0
  29. data/docs/assets/basic.png +0 -0
  30. data/docs/assets/basic_traced.png +0 -0
  31. data/docs/css/main.scss +38 -0
  32. data/docs/favicon.ico +0 -0
  33. data/docs/feed.xml +30 -0
  34. data/docs/index.html +38 -0
  35. data/joonsrenderer.gemspec +23 -0
  36. data/lib/joonsrenderer.rb +12 -0
  37. data/lib/joonsrenderer/version.rb +3 -0
  38. data/pom.rb +75 -0
  39. data/pom.xml +163 -0
  40. data/src/main/java/SunflowGUI.java +1354 -0
  41. data/src/main/java/joons/JRFiller.java +79 -0
  42. data/src/main/java/joons/JRImagePanel.java +141 -0
  43. data/src/main/java/joons/JRRecorder.java +183 -0
  44. data/src/main/java/joons/JRStatics.java +199 -0
  45. data/src/main/java/joons/JoonsRenderer.java +837 -0
  46. data/src/main/java/org/sunflow/AsciiFileSunflowAPI.java +98 -0
  47. data/src/main/java/org/sunflow/Benchmark.java +313 -0
  48. data/src/main/java/org/sunflow/BinaryFileSunflowAPI.java +228 -0
  49. data/src/main/java/org/sunflow/FileSunflowAPI.java +354 -0
  50. data/src/main/java/org/sunflow/PluginRegistry.java +322 -0
  51. data/src/main/java/org/sunflow/RealtimeBenchmark.java +125 -0
  52. data/src/main/java/org/sunflow/RenderObjectMap.java +344 -0
  53. data/src/main/java/org/sunflow/SunflowAPI.java +762 -0
  54. data/src/main/java/org/sunflow/SunflowAPIInterface.java +277 -0
  55. data/src/main/java/org/sunflow/core/AccelerationStructure.java +20 -0
  56. data/src/main/java/org/sunflow/core/AccelerationStructureFactory.java +36 -0
  57. data/src/main/java/org/sunflow/core/BucketOrder.java +21 -0
  58. data/src/main/java/org/sunflow/core/Camera.java +125 -0
  59. data/src/main/java/org/sunflow/core/CameraLens.java +29 -0
  60. data/src/main/java/org/sunflow/core/CausticPhotonMapInterface.java +15 -0
  61. data/src/main/java/org/sunflow/core/Display.java +78 -0
  62. data/src/main/java/org/sunflow/core/Filter.java +27 -0
  63. data/src/main/java/org/sunflow/core/GIEngine.java +42 -0
  64. data/src/main/java/org/sunflow/core/Geometry.java +157 -0
  65. data/src/main/java/org/sunflow/core/GlobalPhotonMapInterface.java +21 -0
  66. data/src/main/java/org/sunflow/core/ImageSampler.java +26 -0
  67. data/src/main/java/org/sunflow/core/Instance.java +224 -0
  68. data/src/main/java/org/sunflow/core/InstanceList.java +83 -0
  69. data/src/main/java/org/sunflow/core/IntersectionState.java +120 -0
  70. data/src/main/java/org/sunflow/core/LightSample.java +104 -0
  71. data/src/main/java/org/sunflow/core/LightServer.java +382 -0
  72. data/src/main/java/org/sunflow/core/LightSource.java +67 -0
  73. data/src/main/java/org/sunflow/core/Modifier.java +16 -0
  74. data/src/main/java/org/sunflow/core/Options.java +20 -0
  75. data/src/main/java/org/sunflow/core/ParameterList.java +758 -0
  76. data/src/main/java/org/sunflow/core/PhotonStore.java +62 -0
  77. data/src/main/java/org/sunflow/core/PrimitiveList.java +70 -0
  78. data/src/main/java/org/sunflow/core/Ray.java +219 -0
  79. data/src/main/java/org/sunflow/core/RenderObject.java +25 -0
  80. data/src/main/java/org/sunflow/core/Scene.java +377 -0
  81. data/src/main/java/org/sunflow/core/SceneParser.java +58 -0
  82. data/src/main/java/org/sunflow/core/Shader.java +30 -0
  83. data/src/main/java/org/sunflow/core/ShadingCache.java +84 -0
  84. data/src/main/java/org/sunflow/core/ShadingState.java +939 -0
  85. data/src/main/java/org/sunflow/core/Statistics.java +85 -0
  86. data/src/main/java/org/sunflow/core/Tesselatable.java +36 -0
  87. data/src/main/java/org/sunflow/core/Texture.java +128 -0
  88. data/src/main/java/org/sunflow/core/TextureCache.java +48 -0
  89. data/src/main/java/org/sunflow/core/accel/BoundingIntervalHierarchy.java +652 -0
  90. data/src/main/java/org/sunflow/core/accel/KDTree.java +833 -0
  91. data/src/main/java/org/sunflow/core/accel/NullAccelerator.java +30 -0
  92. data/src/main/java/org/sunflow/core/accel/UniformGrid.java +329 -0
  93. data/src/main/java/org/sunflow/core/bucket/BucketOrderFactory.java +26 -0
  94. data/src/main/java/org/sunflow/core/bucket/ColumnBucketOrder.java +21 -0
  95. data/src/main/java/org/sunflow/core/bucket/DiagonalBucketOrder.java +28 -0
  96. data/src/main/java/org/sunflow/core/bucket/HilbertBucketOrder.java +65 -0
  97. data/src/main/java/org/sunflow/core/bucket/InvertedBucketOrder.java +28 -0
  98. data/src/main/java/org/sunflow/core/bucket/RandomBucketOrder.java +49 -0
  99. data/src/main/java/org/sunflow/core/bucket/RowBucketOrder.java +21 -0
  100. data/src/main/java/org/sunflow/core/bucket/SpiralBucketOrder.java +43 -0
  101. data/src/main/java/org/sunflow/core/camera/FisheyeLens.java +25 -0
  102. data/src/main/java/org/sunflow/core/camera/PinholeLens.java +43 -0
  103. data/src/main/java/org/sunflow/core/camera/SphericalLens.java +22 -0
  104. data/src/main/java/org/sunflow/core/camera/ThinLens.java +107 -0
  105. data/src/main/java/org/sunflow/core/display/FastDisplay.java +119 -0
  106. data/src/main/java/org/sunflow/core/display/FileDisplay.java +83 -0
  107. data/src/main/java/org/sunflow/core/display/FrameDisplay.java +97 -0
  108. data/src/main/java/org/sunflow/core/display/ImgPipeDisplay.java +109 -0
  109. data/src/main/java/org/sunflow/core/filter/BlackmanHarrisFilter.java +28 -0
  110. data/src/main/java/org/sunflow/core/filter/BoxFilter.java +16 -0
  111. data/src/main/java/org/sunflow/core/filter/CatmullRomFilter.java +29 -0
  112. data/src/main/java/org/sunflow/core/filter/CubicBSpline.java +32 -0
  113. data/src/main/java/org/sunflow/core/filter/GaussianFilter.java +24 -0
  114. data/src/main/java/org/sunflow/core/filter/LanczosFilter.java +30 -0
  115. data/src/main/java/org/sunflow/core/filter/MitchellFilter.java +28 -0
  116. data/src/main/java/org/sunflow/core/filter/SincFilter.java +25 -0
  117. data/src/main/java/org/sunflow/core/filter/TriangleFilter.java +16 -0
  118. data/src/main/java/org/sunflow/core/gi/AmbientOcclusionGIEngine.java +57 -0
  119. data/src/main/java/org/sunflow/core/gi/FakeGIEngine.java +48 -0
  120. data/src/main/java/org/sunflow/core/gi/InstantGI.java +194 -0
  121. data/src/main/java/org/sunflow/core/gi/IrradianceCacheGIEngine.java +268 -0
  122. data/src/main/java/org/sunflow/core/gi/PathTracingGIEngine.java +65 -0
  123. data/src/main/java/org/sunflow/core/light/DirectionalSpotlight.java +103 -0
  124. data/src/main/java/org/sunflow/core/light/ImageBasedLight.java +303 -0
  125. data/src/main/java/org/sunflow/core/light/PointLight.java +72 -0
  126. data/src/main/java/org/sunflow/core/light/SphereLight.java +166 -0
  127. data/src/main/java/org/sunflow/core/light/SunSkyLight.java +362 -0
  128. data/src/main/java/org/sunflow/core/light/TriangleMeshLight.java +296 -0
  129. data/src/main/java/org/sunflow/core/modifiers/BumpMappingModifier.java +37 -0
  130. data/src/main/java/org/sunflow/core/modifiers/NormalMapModifier.java +34 -0
  131. data/src/main/java/org/sunflow/core/modifiers/PerlinModifier.java +80 -0
  132. data/src/main/java/org/sunflow/core/parser/Keyword.java +39 -0
  133. data/src/main/java/org/sunflow/core/parser/RA2Parser.java +107 -0
  134. data/src/main/java/org/sunflow/core/parser/RA3Parser.java +68 -0
  135. data/src/main/java/org/sunflow/core/parser/SCAbstractParser.java +299 -0
  136. data/src/main/java/org/sunflow/core/parser/SCAsciiParser.java +251 -0
  137. data/src/main/java/org/sunflow/core/parser/SCBinaryParser.java +156 -0
  138. data/src/main/java/org/sunflow/core/parser/SCParser.java +1403 -0
  139. data/src/main/java/org/sunflow/core/parser/ShaveRibParser.java +174 -0
  140. data/src/main/java/org/sunflow/core/parser/TriParser.java +79 -0
  141. data/src/main/java/org/sunflow/core/photonmap/CausticPhotonMap.java +429 -0
  142. data/src/main/java/org/sunflow/core/photonmap/GlobalPhotonMap.java +530 -0
  143. data/src/main/java/org/sunflow/core/photonmap/GridPhotonMap.java +308 -0
  144. data/src/main/java/org/sunflow/core/primitive/Background.java +55 -0
  145. data/src/main/java/org/sunflow/core/primitive/BanchoffSurface.java +100 -0
  146. data/src/main/java/org/sunflow/core/primitive/Box.java +210 -0
  147. data/src/main/java/org/sunflow/core/primitive/CornellBox.java +476 -0
  148. data/src/main/java/org/sunflow/core/primitive/CubeGrid.java +318 -0
  149. data/src/main/java/org/sunflow/core/primitive/Cylinder.java +104 -0
  150. data/src/main/java/org/sunflow/core/primitive/Hair.java +275 -0
  151. data/src/main/java/org/sunflow/core/primitive/JuliaFractal.java +266 -0
  152. data/src/main/java/org/sunflow/core/primitive/ParticleSurface.java +114 -0
  153. data/src/main/java/org/sunflow/core/primitive/Plane.java +163 -0
  154. data/src/main/java/org/sunflow/core/primitive/QuadMesh.java +413 -0
  155. data/src/main/java/org/sunflow/core/primitive/Sphere.java +101 -0
  156. data/src/main/java/org/sunflow/core/primitive/SphereFlake.java +234 -0
  157. data/src/main/java/org/sunflow/core/primitive/Torus.java +145 -0
  158. data/src/main/java/org/sunflow/core/primitive/TriangleMesh.java +849 -0
  159. data/src/main/java/org/sunflow/core/renderer/BucketRenderer.java +491 -0
  160. data/src/main/java/org/sunflow/core/renderer/MultipassRenderer.java +237 -0
  161. data/src/main/java/org/sunflow/core/renderer/ProgressiveRenderer.java +171 -0
  162. data/src/main/java/org/sunflow/core/renderer/SimpleRenderer.java +106 -0
  163. data/src/main/java/org/sunflow/core/shader/AmbientOcclusionShader.java +53 -0
  164. data/src/main/java/org/sunflow/core/shader/AnisotropicWardShader.java +216 -0
  165. data/src/main/java/org/sunflow/core/shader/ConstantShader.java +31 -0
  166. data/src/main/java/org/sunflow/core/shader/DiffuseShader.java +65 -0
  167. data/src/main/java/org/sunflow/core/shader/GlassShader.java +147 -0
  168. data/src/main/java/org/sunflow/core/shader/IDShader.java +27 -0
  169. data/src/main/java/org/sunflow/core/shader/MirrorShader.java +68 -0
  170. data/src/main/java/org/sunflow/core/shader/NormalShader.java +32 -0
  171. data/src/main/java/org/sunflow/core/shader/PhongShader.java +89 -0
  172. data/src/main/java/org/sunflow/core/shader/PrimIDShader.java +30 -0
  173. data/src/main/java/org/sunflow/core/shader/QuickGrayShader.java +63 -0
  174. data/src/main/java/org/sunflow/core/shader/ShinyDiffuseShader.java +98 -0
  175. data/src/main/java/org/sunflow/core/shader/SimpleShader.java +24 -0
  176. data/src/main/java/org/sunflow/core/shader/TexturedAmbientOcclusionShader.java +31 -0
  177. data/src/main/java/org/sunflow/core/shader/TexturedDiffuseShader.java +31 -0
  178. data/src/main/java/org/sunflow/core/shader/TexturedPhongShader.java +31 -0
  179. data/src/main/java/org/sunflow/core/shader/TexturedShinyDiffuseShader.java +31 -0
  180. data/src/main/java/org/sunflow/core/shader/TexturedWardShader.java +31 -0
  181. data/src/main/java/org/sunflow/core/shader/UVShader.java +27 -0
  182. data/src/main/java/org/sunflow/core/shader/UberShader.java +149 -0
  183. data/src/main/java/org/sunflow/core/shader/ViewCausticsShader.java +33 -0
  184. data/src/main/java/org/sunflow/core/shader/ViewGlobalPhotonsShader.java +25 -0
  185. data/src/main/java/org/sunflow/core/shader/ViewIrradianceShader.java +25 -0
  186. data/src/main/java/org/sunflow/core/shader/WireframeShader.java +83 -0
  187. data/src/main/java/org/sunflow/core/tesselatable/BezierMesh.java +254 -0
  188. data/src/main/java/org/sunflow/core/tesselatable/FileMesh.java +251 -0
  189. data/src/main/java/org/sunflow/core/tesselatable/Gumbo.java +1147 -0
  190. data/src/main/java/org/sunflow/core/tesselatable/Teapot.java +237 -0
  191. data/src/main/java/org/sunflow/image/Bitmap.java +15 -0
  192. data/src/main/java/org/sunflow/image/BitmapReader.java +39 -0
  193. data/src/main/java/org/sunflow/image/BitmapWriter.java +79 -0
  194. data/src/main/java/org/sunflow/image/BlackbodySpectrum.java +16 -0
  195. data/src/main/java/org/sunflow/image/ChromaticitySpectrum.java +55 -0
  196. data/src/main/java/org/sunflow/image/Color.java +374 -0
  197. data/src/main/java/org/sunflow/image/ColorEncoder.java +94 -0
  198. data/src/main/java/org/sunflow/image/ColorFactory.java +122 -0
  199. data/src/main/java/org/sunflow/image/ConstantSpectralCurve.java +21 -0
  200. data/src/main/java/org/sunflow/image/IrregularSpectralCurve.java +57 -0
  201. data/src/main/java/org/sunflow/image/RGBSpace.java +207 -0
  202. data/src/main/java/org/sunflow/image/RegularSpectralCurve.java +30 -0
  203. data/src/main/java/org/sunflow/image/SpectralCurve.java +118 -0
  204. data/src/main/java/org/sunflow/image/XYZColor.java +50 -0
  205. data/src/main/java/org/sunflow/image/formats/BitmapBlack.java +27 -0
  206. data/src/main/java/org/sunflow/image/formats/BitmapG8.java +36 -0
  207. data/src/main/java/org/sunflow/image/formats/BitmapGA8.java +30 -0
  208. data/src/main/java/org/sunflow/image/formats/BitmapRGB8.java +40 -0
  209. data/src/main/java/org/sunflow/image/formats/BitmapRGBA8.java +40 -0
  210. data/src/main/java/org/sunflow/image/formats/BitmapRGBE.java +60 -0
  211. data/src/main/java/org/sunflow/image/formats/BitmapXYZ.java +38 -0
  212. data/src/main/java/org/sunflow/image/formats/GenericBitmap.java +73 -0
  213. data/src/main/java/org/sunflow/image/readers/BMPBitmapReader.java +39 -0
  214. data/src/main/java/org/sunflow/image/readers/HDRBitmapReader.java +155 -0
  215. data/src/main/java/org/sunflow/image/readers/IGIBitmapReader.java +104 -0
  216. data/src/main/java/org/sunflow/image/readers/JPGBitmapReader.java +39 -0
  217. data/src/main/java/org/sunflow/image/readers/PNGBitmapReader.java +40 -0
  218. data/src/main/java/org/sunflow/image/readers/TGABitmapReader.java +141 -0
  219. data/src/main/java/org/sunflow/image/writers/EXRBitmapWriter.java +395 -0
  220. data/src/main/java/org/sunflow/image/writers/HDRBitmapWriter.java +54 -0
  221. data/src/main/java/org/sunflow/image/writers/IGIBitmapWriter.java +75 -0
  222. data/src/main/java/org/sunflow/image/writers/PNGBitmapWriter.java +39 -0
  223. data/src/main/java/org/sunflow/image/writers/TGABitmapWriter.java +63 -0
  224. data/src/main/java/org/sunflow/math/BoundingBox.java +340 -0
  225. data/src/main/java/org/sunflow/math/MathUtils.java +159 -0
  226. data/src/main/java/org/sunflow/math/Matrix4.java +573 -0
  227. data/src/main/java/org/sunflow/math/MovingMatrix4.java +119 -0
  228. data/src/main/java/org/sunflow/math/OrthoNormalBasis.java +110 -0
  229. data/src/main/java/org/sunflow/math/PerlinScalar.java +331 -0
  230. data/src/main/java/org/sunflow/math/PerlinVector.java +132 -0
  231. data/src/main/java/org/sunflow/math/Point2.java +36 -0
  232. data/src/main/java/org/sunflow/math/Point3.java +133 -0
  233. data/src/main/java/org/sunflow/math/QMC.java +209 -0
  234. data/src/main/java/org/sunflow/math/Solvers.java +142 -0
  235. data/src/main/java/org/sunflow/math/Vector3.java +197 -0
  236. data/src/main/java/org/sunflow/system/BenchmarkFramework.java +73 -0
  237. data/src/main/java/org/sunflow/system/BenchmarkTest.java +17 -0
  238. data/src/main/java/org/sunflow/system/ByteUtil.java +119 -0
  239. data/src/main/java/org/sunflow/system/FileUtils.java +27 -0
  240. data/src/main/java/org/sunflow/system/ImagePanel.java +282 -0
  241. data/src/main/java/org/sunflow/system/Memory.java +18 -0
  242. data/src/main/java/org/sunflow/system/Parser.java +162 -0
  243. data/src/main/java/org/sunflow/system/Plugins.java +142 -0
  244. data/src/main/java/org/sunflow/system/RenderGlobalsPanel.java +209 -0
  245. data/src/main/java/org/sunflow/system/SearchPath.java +67 -0
  246. data/src/main/java/org/sunflow/system/Timer.java +53 -0
  247. data/src/main/java/org/sunflow/system/UI.java +112 -0
  248. data/src/main/java/org/sunflow/system/UserInterface.java +46 -0
  249. data/src/main/java/org/sunflow/system/ui/ConsoleInterface.java +48 -0
  250. data/src/main/java/org/sunflow/system/ui/SilentInterface.java +28 -0
  251. data/src/main/java/org/sunflow/util/FastHashMap.java +220 -0
  252. data/src/main/java/org/sunflow/util/FloatArray.java +77 -0
  253. data/src/main/java/org/sunflow/util/IntArray.java +77 -0
  254. data/src/test/java/a_maintest.java +129 -0
  255. metadata +300 -0
@@ -0,0 +1,159 @@
1
+ package org.sunflow.math;
2
+
3
+ public final class MathUtils {
4
+
5
+ private MathUtils() {
6
+ }
7
+
8
+ public static final int clamp(int x, int min, int max) {
9
+ if (x > max) {
10
+ return max;
11
+ }
12
+ if (x > min) {
13
+ return x;
14
+ }
15
+ return min;
16
+ }
17
+
18
+ public static final float clamp(float x, float min, float max) {
19
+ if (x > max) {
20
+ return max;
21
+ }
22
+ if (x > min) {
23
+ return x;
24
+ }
25
+ return min;
26
+ }
27
+
28
+ public static final double clamp(double x, double min, double max) {
29
+ if (x > max) {
30
+ return max;
31
+ }
32
+ if (x > min) {
33
+ return x;
34
+ }
35
+ return min;
36
+ }
37
+
38
+ public static final int min(int a, int b, int c) {
39
+ if (a > b) {
40
+ a = b;
41
+ }
42
+ if (a > c) {
43
+ a = c;
44
+ }
45
+ return a;
46
+ }
47
+
48
+ public static final float min(float a, float b, float c) {
49
+ if (a > b) {
50
+ a = b;
51
+ }
52
+ if (a > c) {
53
+ a = c;
54
+ }
55
+ return a;
56
+ }
57
+
58
+ public static final double min(double a, double b, double c) {
59
+ if (a > b) {
60
+ a = b;
61
+ }
62
+ if (a > c) {
63
+ a = c;
64
+ }
65
+ return a;
66
+ }
67
+
68
+ public static final float min(float a, float b, float c, float d) {
69
+ if (a > b) {
70
+ a = b;
71
+ }
72
+ if (a > c) {
73
+ a = c;
74
+ }
75
+ if (a > d) {
76
+ a = d;
77
+ }
78
+ return a;
79
+ }
80
+
81
+ public static final int max(int a, int b, int c) {
82
+ if (a < b) {
83
+ a = b;
84
+ }
85
+ if (a < c) {
86
+ a = c;
87
+ }
88
+ return a;
89
+ }
90
+
91
+ public static final float max(float a, float b, float c) {
92
+ if (a < b) {
93
+ a = b;
94
+ }
95
+ if (a < c) {
96
+ a = c;
97
+ }
98
+ return a;
99
+ }
100
+
101
+ public static final double max(double a, double b, double c) {
102
+ if (a < b) {
103
+ a = b;
104
+ }
105
+ if (a < c) {
106
+ a = c;
107
+ }
108
+ return a;
109
+ }
110
+
111
+ public static final float max(float a, float b, float c, float d) {
112
+ if (a < b) {
113
+ a = b;
114
+ }
115
+ if (a < c) {
116
+ a = c;
117
+ }
118
+ if (a < d) {
119
+ a = d;
120
+ }
121
+ return a;
122
+ }
123
+
124
+ public static final float smoothStep(float a, float b, float x) {
125
+ if (x <= a) {
126
+ return 0;
127
+ }
128
+ if (x >= b) {
129
+ return 1;
130
+ }
131
+ float t = clamp((x - a) / (b - a), 0.0f, 1.0f);
132
+ return t * t * (3 - 2 * t);
133
+ }
134
+
135
+ public static final float frac(float x) {
136
+ return x < 0 ? x - (int) x + 1 : x - (int) x;
137
+ }
138
+
139
+ /**
140
+ * Computes a fast approximation to
141
+ * <code>Math.pow(a, b)</code>. Adapted from
142
+ * <url>http://www.dctsystems.co.uk/Software/power.html</url>.
143
+ *
144
+ * @param a a positive number
145
+ * @param b a number
146
+ * @return a^b
147
+ */
148
+ public static final float fastPow(float a, float b) {
149
+ // adapted from: http://www.dctsystems.co.uk/Software/power.html
150
+ float x = Float.floatToRawIntBits(a);
151
+ x *= 1.0f / (1 << 23);
152
+ x = x - 127;
153
+ float y = x - (int) Math.floor(x);
154
+ b *= x + (y - y * y) * 0.346607f;
155
+ y = b - (int) Math.floor(b);
156
+ y = (y - y * y) * 0.33971f;
157
+ return Float.intBitsToFloat((int) ((b + 127 - y) * (1 << 23)));
158
+ }
159
+ }
@@ -0,0 +1,573 @@
1
+ package org.sunflow.math;
2
+
3
+ /**
4
+ * This class is used to represent general affine transformations in 3D. The
5
+ * bottom row of the matrix is assumed to be [0,0,0,1]. Note that the rotation
6
+ * matrices assume a right-handed convention.
7
+ */
8
+ public final class Matrix4 {
9
+ // matrix elements, m(row,col)
10
+
11
+ private float m00;
12
+ private float m01;
13
+ private float m02;
14
+ private float m03;
15
+ private float m10;
16
+ private float m11;
17
+ private float m12;
18
+ private float m13;
19
+ private float m20;
20
+ private float m21;
21
+ private float m22;
22
+ private float m23;
23
+ // usefull constant matrices
24
+ public static final Matrix4 ZERO = new Matrix4();
25
+ public static final Matrix4 IDENTITY = Matrix4.scale(1);
26
+
27
+ /**
28
+ * Creates an empty matrix. All elements are 0.
29
+ */
30
+ private Matrix4() {
31
+ }
32
+
33
+ /**
34
+ * Creates a matrix with the specified elements
35
+ *
36
+ * @param m00 value at row 0, col 0
37
+ * @param m01 value at row 0, col 1
38
+ * @param m02 value at row 0, col 2
39
+ * @param m03 value at row 0, col 3
40
+ * @param m10 value at row 1, col 0
41
+ * @param m11 value at row 1, col 1
42
+ * @param m12 value at row 1, col 2
43
+ * @param m13 value at row 1, col 3
44
+ * @param m20 value at row 2, col 0
45
+ * @param m21 value at row 2, col 1
46
+ * @param m22 value at row 2, col 2
47
+ * @param m23 value at row 2, col 3
48
+ */
49
+ public Matrix4(float m00, float m01, float m02, float m03, float m10, float m11, float m12, float m13, float m20, float m21, float m22, float m23) {
50
+ this.m00 = m00;
51
+ this.m01 = m01;
52
+ this.m02 = m02;
53
+ this.m03 = m03;
54
+ this.m10 = m10;
55
+ this.m11 = m11;
56
+ this.m12 = m12;
57
+ this.m13 = m13;
58
+ this.m20 = m20;
59
+ this.m21 = m21;
60
+ this.m22 = m22;
61
+ this.m23 = m23;
62
+ }
63
+
64
+ /**
65
+ * Initialize a matrix from the specified 16 element array. The matrix may
66
+ * be given in row or column major form.
67
+ *
68
+ * @param m a 16 element array in row or column major form
69
+ * @param rowMajor <code>true</code> if the array is in row major form,
70
+ * <code>false</code>if it is in column major form
71
+ */
72
+ public Matrix4(float[] m, boolean rowMajor) {
73
+ if (rowMajor) {
74
+ m00 = m[0];
75
+ m01 = m[1];
76
+ m02 = m[2];
77
+ m03 = m[3];
78
+ m10 = m[4];
79
+ m11 = m[5];
80
+ m12 = m[6];
81
+ m13 = m[7];
82
+ m20 = m[8];
83
+ m21 = m[9];
84
+ m22 = m[10];
85
+ m23 = m[11];
86
+ if (m[12] != 0 || m[13] != 0 || m[14] != 0 || m[15] != 1) {
87
+ throw new RuntimeException(String.format("Matrix is not affine! Bottom row is: [%.3f, %.3f, %.3f, %.3f]", m[12], m[13], m[14], m[15]));
88
+ }
89
+ } else {
90
+ m00 = m[0];
91
+ m01 = m[4];
92
+ m02 = m[8];
93
+ m03 = m[12];
94
+ m10 = m[1];
95
+ m11 = m[5];
96
+ m12 = m[9];
97
+ m13 = m[13];
98
+ m20 = m[2];
99
+ m21 = m[6];
100
+ m22 = m[10];
101
+ m23 = m[14];
102
+ if (m[3] != 0 || m[7] != 0 || m[11] != 0 || m[15] != 1) {
103
+ throw new RuntimeException(String.format("Matrix is not affine! Bottom row is: [%.3f, %.3f, %.3f, %.3f]", m[12], m[13], m[14], m[15]));
104
+ }
105
+ }
106
+ }
107
+
108
+ public final boolean isIndentity() {
109
+ return equals(IDENTITY);
110
+ }
111
+
112
+ public final boolean equals(Matrix4 m) {
113
+ if (m == null) {
114
+ return false;
115
+ }
116
+ if (this == m) {
117
+ return true;
118
+ }
119
+ return m00 == m.m00 && m01 == m.m01 && m02 == m.m02 && m03 == m.m03 && m10 == m.m10 && m11 == m.m11 && m12 == m.m12 && m13 == m.m13 && m20 == m.m20 && m21 == m.m21 && m22 == m.m22 && m23 == m.m23;
120
+ }
121
+
122
+ public final float[] asRowMajor() {
123
+ return new float[]{m00, m01, m02, m03, m10, m11, m12, m13, m20, m21,
124
+ m22, m23, 0, 0, 0, 1};
125
+ }
126
+
127
+ public final float[] asColMajor() {
128
+ return new float[]{m00, m10, m20, 0, m01, m11, m21, 0, m02, m12, m22,
129
+ 0, m03, m13, m23, 1};
130
+ }
131
+
132
+ /**
133
+ * Compute the matrix determinant.
134
+ *
135
+ * @return determinant of this matrix
136
+ */
137
+ public final float determinant() {
138
+ float A0 = m00 * m11 - m01 * m10;
139
+ float A1 = m00 * m12 - m02 * m10;
140
+ float A3 = m01 * m12 - m02 * m11;
141
+
142
+ return A0 * m22 - A1 * m21 + A3 * m20;
143
+ }
144
+
145
+ /**
146
+ * Compute the inverse of this matrix and return it as a new object. If the
147
+ * matrix is not invertible,
148
+ * <code>null</code> is returned.
149
+ *
150
+ * @return the inverse of this matrix, or <code>null</code> if not
151
+ * invertible
152
+ */
153
+ public final Matrix4 inverse() {
154
+ float A0 = m00 * m11 - m01 * m10;
155
+ float A1 = m00 * m12 - m02 * m10;
156
+ float A3 = m01 * m12 - m02 * m11;
157
+ float det = A0 * m22 - A1 * m21 + A3 * m20;
158
+ if (Math.abs(det) < 1e-12f) {
159
+ return null; // matrix is not invertible
160
+ }
161
+ float invDet = 1 / det;
162
+ float A2 = m00 * m13 - m03 * m10;
163
+ float A4 = m01 * m13 - m03 * m11;
164
+ float A5 = m02 * m13 - m03 * m12;
165
+ Matrix4 inv = new Matrix4();
166
+ inv.m00 = (+m11 * m22 - m12 * m21) * invDet;
167
+ inv.m10 = (-m10 * m22 + m12 * m20) * invDet;
168
+ inv.m20 = (+m10 * m21 - m11 * m20) * invDet;
169
+ inv.m01 = (-m01 * m22 + m02 * m21) * invDet;
170
+ inv.m11 = (+m00 * m22 - m02 * m20) * invDet;
171
+ inv.m21 = (-m00 * m21 + m01 * m20) * invDet;
172
+ inv.m02 = +A3 * invDet;
173
+ inv.m12 = -A1 * invDet;
174
+ inv.m22 = +A0 * invDet;
175
+ inv.m03 = (-m21 * A5 + m22 * A4 - m23 * A3) * invDet;
176
+ inv.m13 = (+m20 * A5 - m22 * A2 + m23 * A1) * invDet;
177
+ inv.m23 = (-m20 * A4 + m21 * A2 - m23 * A0) * invDet;
178
+ return inv;
179
+ }
180
+
181
+ /**
182
+ * Computes this*m and return the result as a new Matrix4
183
+ *
184
+ * @param m right hand side of the multiplication
185
+ * @return a new Matrix4 object equal to <code>this*m</code>
186
+ */
187
+ public final Matrix4 multiply(Matrix4 m) {
188
+ // matrix multiplication is m[r][c] = (row[r]).(col[c])
189
+ float rm00 = m00 * m.m00 + m01 * m.m10 + m02 * m.m20;
190
+ float rm01 = m00 * m.m01 + m01 * m.m11 + m02 * m.m21;
191
+ float rm02 = m00 * m.m02 + m01 * m.m12 + m02 * m.m22;
192
+ float rm03 = m00 * m.m03 + m01 * m.m13 + m02 * m.m23 + m03;
193
+
194
+ float rm10 = m10 * m.m00 + m11 * m.m10 + m12 * m.m20;
195
+ float rm11 = m10 * m.m01 + m11 * m.m11 + m12 * m.m21;
196
+ float rm12 = m10 * m.m02 + m11 * m.m12 + m12 * m.m22;
197
+ float rm13 = m10 * m.m03 + m11 * m.m13 + m12 * m.m23 + m13;
198
+
199
+ float rm20 = m20 * m.m00 + m21 * m.m10 + m22 * m.m20;
200
+ float rm21 = m20 * m.m01 + m21 * m.m11 + m22 * m.m21;
201
+ float rm22 = m20 * m.m02 + m21 * m.m12 + m22 * m.m22;
202
+ float rm23 = m20 * m.m03 + m21 * m.m13 + m22 * m.m23 + m23;
203
+
204
+ return new Matrix4(rm00, rm01, rm02, rm03, rm10, rm11, rm12, rm13, rm20, rm21, rm22, rm23);
205
+ }
206
+
207
+ /**
208
+ * Transforms each corner of the specified axis-aligned bounding box and
209
+ * returns a new bounding box which incloses the transformed corners.
210
+ *
211
+ * @param b original bounding box
212
+ * @return a new BoundingBox object which encloses the transform version of
213
+ * b
214
+ */
215
+ public final BoundingBox transform(BoundingBox b) {
216
+ if (b.isEmpty()) {
217
+ return new BoundingBox();
218
+ }
219
+ // special case extreme corners
220
+ BoundingBox rb = new BoundingBox(transformP(b.getMinimum()));
221
+ rb.include(transformP(b.getMaximum()));
222
+ // do internal corners
223
+ for (int i = 1; i < 7; i++) {
224
+ rb.include(transformP(b.getCorner(i)));
225
+ }
226
+ return rb;
227
+ }
228
+
229
+ /**
230
+ * Computes this*v and returns the result as a new Vector3 object. This
231
+ * method assumes the bottom row of the matrix is
232
+ * <code>[0,0,0,1]</code>.
233
+ *
234
+ * @param v vector to multiply
235
+ * @return a new Vector3 object equal to <code>this*v</code>
236
+ */
237
+ public final Vector3 transformV(Vector3 v) {
238
+ Vector3 rv = new Vector3();
239
+ rv.x = m00 * v.x + m01 * v.y + m02 * v.z;
240
+ rv.y = m10 * v.x + m11 * v.y + m12 * v.z;
241
+ rv.z = m20 * v.x + m21 * v.y + m22 * v.z;
242
+ return rv;
243
+ }
244
+
245
+ /**
246
+ * Computes (this^T)*v and returns the result as a new Vector3 object. This
247
+ * method assumes the bottom row of the matrix is
248
+ * <code>[0,0,0,1]</code>.
249
+ *
250
+ * @param v vector to multiply
251
+ * @return a new Vector3 object equal to <code>(this^T)*v</code>
252
+ */
253
+ public final Vector3 transformTransposeV(Vector3 v) {
254
+ Vector3 rv = new Vector3();
255
+ rv.x = m00 * v.x + m10 * v.y + m20 * v.z;
256
+ rv.y = m01 * v.x + m11 * v.y + m21 * v.z;
257
+ rv.z = m02 * v.x + m12 * v.y + m22 * v.z;
258
+ return rv;
259
+ }
260
+
261
+ /**
262
+ * Computes this*p and returns the result as a new Point3 object. This
263
+ * method assumes the bottom row of the matrix is
264
+ * <code>[0,0,0,1]</code>.
265
+ *
266
+ * @param p point to multiply
267
+ * @return a new Point3 object equal to <code>this*v</code>
268
+ */
269
+ public final Point3 transformP(Point3 p) {
270
+ Point3 rp = new Point3();
271
+ rp.x = m00 * p.x + m01 * p.y + m02 * p.z + m03;
272
+ rp.y = m10 * p.x + m11 * p.y + m12 * p.z + m13;
273
+ rp.z = m20 * p.x + m21 * p.y + m22 * p.z + m23;
274
+ return rp;
275
+ }
276
+
277
+ /**
278
+ * Computes the x component of this*(x,y,z,0).
279
+ *
280
+ * @param x x coordinate of the vector to multiply
281
+ * @param y y coordinate of the vector to multiply
282
+ * @param z z coordinate of the vector to multiply
283
+ * @return x coordinate transformation result
284
+ */
285
+ public final float transformVX(float x, float y, float z) {
286
+ return m00 * x + m01 * y + m02 * z;
287
+ }
288
+
289
+ /**
290
+ * Computes the y component of this*(x,y,z,0).
291
+ *
292
+ * @param x x coordinate of the vector to multiply
293
+ * @param y y coordinate of the vector to multiply
294
+ * @param z z coordinate of the vector to multiply
295
+ * @return y coordinate transformation result
296
+ */
297
+ public final float transformVY(float x, float y, float z) {
298
+ return m10 * x + m11 * y + m12 * z;
299
+ }
300
+
301
+ /**
302
+ * Computes the z component of this*(x,y,z,0).
303
+ *
304
+ * @param x x coordinate of the vector to multiply
305
+ * @param y y coordinate of the vector to multiply
306
+ * @param z z coordinate of the vector to multiply
307
+ * @return z coordinate transformation result
308
+ */
309
+ public final float transformVZ(float x, float y, float z) {
310
+ return m20 * x + m21 * y + m22 * z;
311
+ }
312
+
313
+ /**
314
+ * Computes the x component of (this^T)*(x,y,z,0).
315
+ *
316
+ * @param x x coordinate of the vector to multiply
317
+ * @param y y coordinate of the vector to multiply
318
+ * @param z z coordinate of the vector to multiply
319
+ * @return x coordinate transformation result
320
+ */
321
+ public final float transformTransposeVX(float x, float y, float z) {
322
+ return m00 * x + m10 * y + m20 * z;
323
+ }
324
+
325
+ /**
326
+ * Computes the y component of (this^T)*(x,y,z,0).
327
+ *
328
+ * @param x x coordinate of the vector to multiply
329
+ * @param y y coordinate of the vector to multiply
330
+ * @param z z coordinate of the vector to multiply
331
+ * @return y coordinate transformation result
332
+ */
333
+ public final float transformTransposeVY(float x, float y, float z) {
334
+ return m01 * x + m11 * y + m21 * z;
335
+ }
336
+
337
+ /**
338
+ * Computes the z component of (this^T)*(x,y,z,0).
339
+ *
340
+ * @param x x coordinate of the vector to multiply
341
+ * @param y y coordinate of the vector to multiply
342
+ * @param z z coordinate of the vector to multiply
343
+ * @return zcoordinate transformation result
344
+ */
345
+ public final float transformTransposeVZ(float x, float y, float z) {
346
+ return m02 * x + m12 * y + m22 * z;
347
+ }
348
+
349
+ /**
350
+ * Computes the x component of this*(x,y,z,1).
351
+ *
352
+ * @param x x coordinate of the vector to multiply
353
+ * @param y y coordinate of the vector to multiply
354
+ * @param z z coordinate of the vector to multiply
355
+ * @return x coordinate transformation result
356
+ */
357
+ public final float transformPX(float x, float y, float z) {
358
+ return m00 * x + m01 * y + m02 * z + m03;
359
+ }
360
+
361
+ /**
362
+ * Computes the y component of this*(x,y,z,1).
363
+ *
364
+ * @param x x coordinate of the vector to multiply
365
+ * @param y y coordinate of the vector to multiply
366
+ * @param z z coordinate of the vector to multiply
367
+ * @return y coordinate transformation result
368
+ */
369
+ public final float transformPY(float x, float y, float z) {
370
+ return m10 * x + m11 * y + m12 * z + m13;
371
+ }
372
+
373
+ /**
374
+ * Computes the z component of this*(x,y,z,1).
375
+ *
376
+ * @param x x coordinate of the vector to multiply
377
+ * @param y y coordinate of the vector to multiply
378
+ * @param z z coordinate of the vector to multiply
379
+ * @return z coordinate transformation result
380
+ */
381
+ public final float transformPZ(float x, float y, float z) {
382
+ return m20 * x + m21 * y + m22 * z + m23;
383
+ }
384
+
385
+ /**
386
+ * Create a translation matrix for the specified vector.
387
+ *
388
+ * @param x x component of translation
389
+ * @param y y component of translation
390
+ * @param z z component of translation
391
+ * @return a new Matrix4 object representing the translation
392
+ */
393
+ public final static Matrix4 translation(float x, float y, float z) {
394
+ Matrix4 m = new Matrix4();
395
+ m.m00 = m.m11 = m.m22 = 1;
396
+ m.m03 = x;
397
+ m.m13 = y;
398
+ m.m23 = z;
399
+ return m;
400
+ }
401
+
402
+ /**
403
+ * Creates a rotation matrix about the X axis.
404
+ *
405
+ * @param theta angle to rotate about the X axis in radians
406
+ * @return a new Matrix4 object representing the rotation
407
+ */
408
+ public final static Matrix4 rotateX(float theta) {
409
+ Matrix4 m = new Matrix4();
410
+ float s = (float) Math.sin(theta);
411
+ float c = (float) Math.cos(theta);
412
+ m.m00 = 1;
413
+ m.m11 = m.m22 = c;
414
+ m.m12 = -s;
415
+ m.m21 = +s;
416
+ return m;
417
+ }
418
+
419
+ /**
420
+ * Creates a rotation matrix about the Y axis.
421
+ *
422
+ * @param theta angle to rotate about the Y axis in radians
423
+ * @return a new Matrix4 object representing the rotation
424
+ */
425
+ public final static Matrix4 rotateY(float theta) {
426
+ Matrix4 m = new Matrix4();
427
+ float s = (float) Math.sin(theta);
428
+ float c = (float) Math.cos(theta);
429
+ m.m11 = 1;
430
+ m.m00 = m.m22 = c;
431
+ m.m02 = +s;
432
+ m.m20 = -s;
433
+ return m;
434
+ }
435
+
436
+ /**
437
+ * Creates a rotation matrix about the Z axis.
438
+ *
439
+ * @param theta angle to rotate about the Z axis in radians
440
+ * @return a new Matrix4 object representing the rotation
441
+ */
442
+ public final static Matrix4 rotateZ(float theta) {
443
+ Matrix4 m = new Matrix4();
444
+ float s = (float) Math.sin(theta);
445
+ float c = (float) Math.cos(theta);
446
+ m.m22 = 1;
447
+ m.m00 = m.m11 = c;
448
+ m.m01 = -s;
449
+ m.m10 = +s;
450
+ return m;
451
+ }
452
+
453
+ /**
454
+ * Creates a rotation matrix about the specified axis. The axis vector need
455
+ * not be normalized.
456
+ *
457
+ * @param x x component of the axis vector
458
+ * @param y y component of the axis vector
459
+ * @param z z component of the axis vector
460
+ * @param theta angle to rotate about the axis in radians
461
+ * @return a new Matrix4 object representing the rotation
462
+ */
463
+ public final static Matrix4 rotate(float x, float y, float z, float theta) {
464
+ Matrix4 m = new Matrix4();
465
+ float invLen = 1 / (float) Math.sqrt(x * x + y * y + z * z);
466
+ x *= invLen;
467
+ y *= invLen;
468
+ z *= invLen;
469
+ float s = (float) Math.sin(theta);
470
+ float c = (float) Math.cos(theta);
471
+ float t = 1 - c;
472
+ m.m00 = t * x * x + c;
473
+ m.m11 = t * y * y + c;
474
+ m.m22 = t * z * z + c;
475
+ float txy = t * x * y;
476
+ float sz = s * z;
477
+ m.m01 = txy - sz;
478
+ m.m10 = txy + sz;
479
+ float txz = t * x * z;
480
+ float sy = s * y;
481
+ m.m02 = txz + sy;
482
+ m.m20 = txz - sy;
483
+ float tyz = t * y * z;
484
+ float sx = s * x;
485
+ m.m12 = tyz - sx;
486
+ m.m21 = tyz + sx;
487
+ return m;
488
+ }
489
+
490
+ /**
491
+ * Create a uniform scaling matrix.
492
+ *
493
+ * @param s scale factor for all three axes
494
+ * @return a new Matrix4 object representing the uniform scale
495
+ */
496
+ public final static Matrix4 scale(float s) {
497
+ Matrix4 m = new Matrix4();
498
+ m.m00 = m.m11 = m.m22 = s;
499
+ return m;
500
+ }
501
+
502
+ /**
503
+ * Creates a non-uniform scaling matrix.
504
+ *
505
+ * @param sx scale factor in the x dimension
506
+ * @param sy scale factor in the y dimension
507
+ * @param sz scale factor in the z dimension
508
+ * @return a new Matrix4 object representing the non-uniform scale
509
+ */
510
+ public final static Matrix4 scale(float sx, float sy, float sz) {
511
+ Matrix4 m = new Matrix4();
512
+ m.m00 = sx;
513
+ m.m11 = sy;
514
+ m.m22 = sz;
515
+ return m;
516
+ }
517
+
518
+ /**
519
+ * Creates a rotation matrix from an OrthonormalBasis.
520
+ *
521
+ * @param basis
522
+ * @return Matrix4
523
+ */
524
+ public final static Matrix4 fromBasis(OrthoNormalBasis basis) {
525
+ Matrix4 m = new Matrix4();
526
+ Vector3 u = basis.transform(new Vector3(1, 0, 0));
527
+ Vector3 v = basis.transform(new Vector3(0, 1, 0));
528
+ Vector3 w = basis.transform(new Vector3(0, 0, 1));
529
+ m.m00 = u.x;
530
+ m.m01 = v.x;
531
+ m.m02 = w.x;
532
+ m.m10 = u.y;
533
+ m.m11 = v.y;
534
+ m.m12 = w.y;
535
+ m.m20 = u.z;
536
+ m.m21 = v.z;
537
+ m.m22 = w.z;
538
+ return m;
539
+ }
540
+
541
+ /**
542
+ * Creates a camera positioning matrix from the given eye and target points
543
+ * and up vector.
544
+ *
545
+ * @param eye location of the eye
546
+ * @param target location of the target
547
+ * @param up vector pointing upwards
548
+ * @return
549
+ */
550
+ public final static Matrix4 lookAt(Point3 eye, Point3 target, Vector3 up) {
551
+ Matrix4 m = Matrix4.fromBasis(OrthoNormalBasis.makeFromWV(Point3.sub(eye, target, new Vector3()), up));
552
+ return Matrix4.translation(eye.x, eye.y, eye.z).multiply(m);
553
+ }
554
+
555
+ public final static Matrix4 blend(Matrix4 m0, Matrix4 m1, float t) {
556
+ Matrix4 m = new Matrix4();
557
+ m.m00 = (1 - t) * m0.m00 + t * m1.m00;
558
+ m.m01 = (1 - t) * m0.m01 + t * m1.m01;
559
+ m.m02 = (1 - t) * m0.m02 + t * m1.m02;
560
+ m.m03 = (1 - t) * m0.m03 + t * m1.m03;
561
+
562
+ m.m10 = (1 - t) * m0.m10 + t * m1.m10;
563
+ m.m11 = (1 - t) * m0.m11 + t * m1.m11;
564
+ m.m12 = (1 - t) * m0.m12 + t * m1.m12;
565
+ m.m13 = (1 - t) * m0.m13 + t * m1.m13;
566
+
567
+ m.m20 = (1 - t) * m0.m20 + t * m1.m20;
568
+ m.m21 = (1 - t) * m0.m21 + t * m1.m21;
569
+ m.m22 = (1 - t) * m0.m22 + t * m1.m22;
570
+ m.m23 = (1 - t) * m0.m23 + t * m1.m23;
571
+ return m;
572
+ }
573
+ }