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,142 @@
1
+ package org.sunflow.math;
2
+
3
+ public final class Solvers {
4
+
5
+ /**
6
+ * Solves the equation ax^2+bx+c=0. Solutions are returned in a sorted array
7
+ * if they exist.
8
+ *
9
+ * @param a coefficient of x^2
10
+ * @param b coefficient of x^1
11
+ * @param c coefficient of x^0
12
+ * @return an array containing the two real roots, or <code>null</code> if
13
+ * no real solutions exist
14
+ */
15
+ public static double[] solveQuadric(double a, double b, double c) {
16
+ double disc = b * b - 4 * a * c;
17
+ if (disc < 0) {
18
+ return null;
19
+ }
20
+ disc = Math.sqrt(disc);
21
+ double q = ((b < 0) ? -0.5 * (b - disc) : -0.5 * (b + disc));
22
+ double t0 = q / a;
23
+ double t1 = c / q;
24
+ // return sorted array
25
+ return (t0 > t1) ? new double[]{t1, t0} : new double[]{t0, t1};
26
+ }
27
+
28
+ /**
29
+ * Solve a quartic equation of the form ax^4+bx^3+cx^2+cx^1+d=0. The roots
30
+ * are returned in a sorted array of doubles in increasing order.
31
+ *
32
+ * @param a coefficient of x^4
33
+ * @param b coefficient of x^3
34
+ * @param c coefficient of x^2
35
+ * @param d coefficient of x^1
36
+ * @param e coefficient of x^0
37
+ * @return a sorted array of roots, or <code>null</code> if no solutions
38
+ * exist
39
+ */
40
+ public static double[] solveQuartic(double a, double b, double c, double d, double e) {
41
+ double inva = 1 / a;
42
+ double c1 = b * inva;
43
+ double c2 = c * inva;
44
+ double c3 = d * inva;
45
+ double c4 = e * inva;
46
+ // cubic resolvant
47
+ double c12 = c1 * c1;
48
+ double p = -0.375 * c12 + c2;
49
+ double q = 0.125 * c12 * c1 - 0.5 * c1 * c2 + c3;
50
+ double r = -0.01171875 * c12 * c12 + 0.0625 * c12 * c2 - 0.25 * c1 * c3 + c4;
51
+ double z = solveCubicForQuartic(-0.5 * p, -r, 0.5 * r * p - 0.125 * q * q);
52
+ double d1 = 2.0 * z - p;
53
+ if (d1 < 0) {
54
+ if (d1 > 1.0e-10) {
55
+ d1 = 0;
56
+ } else {
57
+ return null;
58
+ }
59
+ }
60
+ double d2;
61
+ if (d1 < 1.0e-10) {
62
+ d2 = z * z - r;
63
+ if (d2 < 0) {
64
+ return null;
65
+ }
66
+ d2 = Math.sqrt(d2);
67
+ } else {
68
+ d1 = Math.sqrt(d1);
69
+ d2 = 0.5 * q / d1;
70
+ }
71
+ // setup usefull values for the quadratic factors
72
+ double q1 = d1 * d1;
73
+ double q2 = -0.25 * c1;
74
+ double pm = q1 - 4 * (z - d2);
75
+ double pp = q1 - 4 * (z + d2);
76
+ if (pm >= 0 && pp >= 0) {
77
+ // 4 roots (!)
78
+ pm = Math.sqrt(pm);
79
+ pp = Math.sqrt(pp);
80
+ double[] results = new double[4];
81
+ results[0] = -0.5 * (d1 + pm) + q2;
82
+ results[1] = -0.5 * (d1 - pm) + q2;
83
+ results[2] = 0.5 * (d1 + pp) + q2;
84
+ results[3] = 0.5 * (d1 - pp) + q2;
85
+ // tiny insertion sort
86
+ for (int i = 1; i < 4; i++) {
87
+ for (int j = i; j > 0 && results[j - 1] > results[j]; j--) {
88
+ double t = results[j];
89
+ results[j] = results[j - 1];
90
+ results[j - 1] = t;
91
+ }
92
+ }
93
+ return results;
94
+ } else if (pm >= 0) {
95
+ pm = Math.sqrt(pm);
96
+ double[] results = new double[2];
97
+ results[0] = -0.5 * (d1 + pm) + q2;
98
+ results[1] = -0.5 * (d1 - pm) + q2;
99
+ return results;
100
+ } else if (pp >= 0) {
101
+ pp = Math.sqrt(pp);
102
+ double[] results = new double[2];
103
+ results[0] = 0.5 * (d1 - pp) + q2;
104
+ results[1] = 0.5 * (d1 + pp) + q2;
105
+ return results;
106
+ }
107
+ return null;
108
+ }
109
+
110
+ /**
111
+ * Return only one root for the specified cubic equation. This routine is
112
+ * only meant to be called by the quartic solver. It assumes the cubic is of
113
+ * the form: x^3+px^2+qx+r.
114
+ *
115
+ * @param p
116
+ * @param q
117
+ * @param r
118
+ * @return
119
+ */
120
+ private static double solveCubicForQuartic(double p, double q, double r) {
121
+ double A2 = p * p;
122
+ double Q = (A2 - 3.0 * q) / 9.0;
123
+ double R = (p * (A2 - 4.5 * q) + 13.5 * r) / 27.0;
124
+ double Q3 = Q * Q * Q;
125
+ double R2 = R * R;
126
+ double d = Q3 - R2;
127
+ double an = p / 3.0;
128
+ if (d >= 0) {
129
+ d = R / Math.sqrt(Q3);
130
+ double theta = Math.acos(d) / 3.0;
131
+ double sQ = -2.0 * Math.sqrt(Q);
132
+ return sQ * Math.cos(theta) - an;
133
+ } else {
134
+ double sQ = Math.pow(Math.sqrt(R2 - Q3) + Math.abs(R), 1.0 / 3.0);
135
+ if (R < 0) {
136
+ return (sQ + Q / sQ) - an;
137
+ } else {
138
+ return -(sQ + Q / sQ) - an;
139
+ }
140
+ }
141
+ }
142
+ }
@@ -0,0 +1,197 @@
1
+ package org.sunflow.math;
2
+
3
+ public final class Vector3 {
4
+
5
+ private static final float[] COS_THETA = new float[256];
6
+ private static final float[] SIN_THETA = new float[256];
7
+ private static final float[] COS_PHI = new float[256];
8
+ private static final float[] SIN_PHI = new float[256];
9
+ public float x, y, z;
10
+
11
+ static {
12
+ // precompute tables to compress unit vectors
13
+ for (int i = 0; i < 256; i++) {
14
+ double angle = (i * Math.PI) / 256.0;
15
+ COS_THETA[i] = (float) Math.cos(angle);
16
+ SIN_THETA[i] = (float) Math.sin(angle);
17
+ COS_PHI[i] = (float) Math.cos(2 * angle);
18
+ SIN_PHI[i] = (float) Math.sin(2 * angle);
19
+ }
20
+ }
21
+
22
+ public Vector3() {
23
+ }
24
+
25
+ public Vector3(float x, float y, float z) {
26
+ this.x = x;
27
+ this.y = y;
28
+ this.z = z;
29
+ }
30
+
31
+ public Vector3(Vector3 v) {
32
+ x = v.x;
33
+ y = v.y;
34
+ z = v.z;
35
+ }
36
+
37
+ public static final Vector3 decode(short n, Vector3 dest) {
38
+ int t = (n & 0xFF00) >>> 8;
39
+ int p = n & 0xFF;
40
+ dest.x = SIN_THETA[t] * COS_PHI[p];
41
+ dest.y = SIN_THETA[t] * SIN_PHI[p];
42
+ dest.z = COS_THETA[t];
43
+ return dest;
44
+ }
45
+
46
+ public static final Vector3 decode(short n) {
47
+ return decode(n, new Vector3());
48
+ }
49
+
50
+ public final short encode() {
51
+ int theta = (int) (Math.acos(z) * (256.0 / Math.PI));
52
+ if (theta > 255) {
53
+ theta = 255;
54
+ }
55
+ int phi = (int) (Math.atan2(y, x) * (128.0 / Math.PI));
56
+ if (phi < 0) {
57
+ phi += 256;
58
+ } else if (phi > 255) {
59
+ phi = 255;
60
+ }
61
+ return (short) (((theta & 0xFF) << 8) | (phi & 0xFF));
62
+ }
63
+
64
+ public float get(int i) {
65
+ switch (i) {
66
+ case 0:
67
+ return x;
68
+ case 1:
69
+ return y;
70
+ default:
71
+ return z;
72
+ }
73
+ }
74
+
75
+ public final float length() {
76
+ return (float) Math.sqrt((x * x) + (y * y) + (z * z));
77
+ }
78
+
79
+ public final float lengthSquared() {
80
+ return (x * x) + (y * y) + (z * z);
81
+ }
82
+
83
+ public final Vector3 negate() {
84
+ x = -x;
85
+ y = -y;
86
+ z = -z;
87
+ return this;
88
+ }
89
+
90
+ public final Vector3 negate(Vector3 dest) {
91
+ dest.x = -x;
92
+ dest.y = -y;
93
+ dest.z = -z;
94
+ return dest;
95
+ }
96
+
97
+ public final Vector3 mul(float s) {
98
+ x *= s;
99
+ y *= s;
100
+ z *= s;
101
+ return this;
102
+ }
103
+
104
+ public final Vector3 mul(float s, Vector3 dest) {
105
+ dest.x = x * s;
106
+ dest.y = y * s;
107
+ dest.z = z * s;
108
+ return dest;
109
+ }
110
+
111
+ public final Vector3 div(float d) {
112
+ x /= d;
113
+ y /= d;
114
+ z /= d;
115
+ return this;
116
+ }
117
+
118
+ public final Vector3 div(float d, Vector3 dest) {
119
+ dest.x = x / d;
120
+ dest.y = y / d;
121
+ dest.z = z / d;
122
+ return dest;
123
+ }
124
+
125
+ public final float normalizeLength() {
126
+ float n = (float) Math.sqrt(x * x + y * y + z * z);
127
+ float in = 1.0f / n;
128
+ x *= in;
129
+ y *= in;
130
+ z *= in;
131
+ return n;
132
+ }
133
+
134
+ public final Vector3 normalize() {
135
+ float in = 1.0f / (float) Math.sqrt((x * x) + (y * y) + (z * z));
136
+ x *= in;
137
+ y *= in;
138
+ z *= in;
139
+ return this;
140
+ }
141
+
142
+ public final Vector3 normalize(Vector3 dest) {
143
+ float in = 1.0f / (float) Math.sqrt((x * x) + (y * y) + (z * z));
144
+ dest.x = x * in;
145
+ dest.y = y * in;
146
+ dest.z = z * in;
147
+ return dest;
148
+ }
149
+
150
+ public final Vector3 set(float x, float y, float z) {
151
+ this.x = x;
152
+ this.y = y;
153
+ this.z = z;
154
+ return this;
155
+ }
156
+
157
+ public final Vector3 set(Vector3 v) {
158
+ x = v.x;
159
+ y = v.y;
160
+ z = v.z;
161
+ return this;
162
+ }
163
+
164
+ public final float dot(float vx, float vy, float vz) {
165
+ return vx * x + vy * y + vz * z;
166
+ }
167
+
168
+ public static final float dot(Vector3 v1, Vector3 v2) {
169
+ return (v1.x * v2.x) + (v1.y * v2.y) + (v1.z * v2.z);
170
+ }
171
+
172
+ public static final Vector3 cross(Vector3 v1, Vector3 v2, Vector3 dest) {
173
+ dest.x = (v1.y * v2.z) - (v1.z * v2.y);
174
+ dest.y = (v1.z * v2.x) - (v1.x * v2.z);
175
+ dest.z = (v1.x * v2.y) - (v1.y * v2.x);
176
+ return dest;
177
+ }
178
+
179
+ public static final Vector3 add(Vector3 v1, Vector3 v2, Vector3 dest) {
180
+ dest.x = v1.x + v2.x;
181
+ dest.y = v1.y + v2.y;
182
+ dest.z = v1.z + v2.z;
183
+ return dest;
184
+ }
185
+
186
+ public static final Vector3 sub(Vector3 v1, Vector3 v2, Vector3 dest) {
187
+ dest.x = v1.x - v2.x;
188
+ dest.y = v1.y - v2.y;
189
+ dest.z = v1.z - v2.z;
190
+ return dest;
191
+ }
192
+
193
+ @Override
194
+ public final String toString() {
195
+ return String.format("(%.2f, %.2f, %.2f)", x, y, z);
196
+ }
197
+ }
@@ -0,0 +1,73 @@
1
+ package org.sunflow.system;
2
+
3
+ import org.sunflow.system.UI.Module;
4
+
5
+ /**
6
+ * This class provides a very simple framework for running a BenchmarkTest
7
+ * kernel several times and time the results.
8
+ */
9
+ public class BenchmarkFramework {
10
+
11
+ private Timer[] timers;
12
+ private int timeLimit; // time limit in seconds
13
+
14
+ public BenchmarkFramework(int iterations, int timeLimit) {
15
+ this.timeLimit = timeLimit;
16
+ timers = new Timer[iterations];
17
+ }
18
+
19
+ public void execute(BenchmarkTest test) {
20
+ // clear previous results
21
+ for (int i = 0; i < timers.length; i++) {
22
+ timers[i] = null;
23
+ }
24
+ // loop for the specified number of iterations or until the time limit
25
+ long startTime = System.nanoTime();
26
+ for (int i = 0; i < timers.length && ((System.nanoTime() - startTime) / 1000000000) < timeLimit; i++) {
27
+ UI.printInfo(Module.BENCH, "Running iteration %d", (i + 1));
28
+ timers[i] = new Timer();
29
+ test.kernelBegin();
30
+ timers[i].start();
31
+ test.kernelMain();
32
+ timers[i].end();
33
+ test.kernelEnd();
34
+ }
35
+ // report stats
36
+ double avg = 0;
37
+ double min = Double.POSITIVE_INFINITY;
38
+ double max = Double.NEGATIVE_INFINITY;
39
+ int n = 0;
40
+ for (Timer t : timers) {
41
+ if (t == null) {
42
+ break;
43
+ }
44
+ double s = t.seconds();
45
+ min = Math.min(min, s);
46
+ max = Math.max(max, s);
47
+ avg += s;
48
+ n++;
49
+ }
50
+ if (n == 0) {
51
+ return;
52
+ }
53
+ avg /= n;
54
+ double stdDev = 0;
55
+ for (Timer t : timers) {
56
+ if (t == null) {
57
+ break;
58
+ }
59
+ double s = t.seconds();
60
+ stdDev += (s - avg) * (s - avg);
61
+ }
62
+ stdDev = Math.sqrt(stdDev / n);
63
+ UI.printInfo(Module.BENCH, "Benchmark results:");
64
+ UI.printInfo(Module.BENCH, " * Iterations: %d", n);
65
+ UI.printInfo(Module.BENCH, " * Average: %s", Timer.toString(avg));
66
+ UI.printInfo(Module.BENCH, " * Fastest: %s", Timer.toString(min));
67
+ UI.printInfo(Module.BENCH, " * Longest: %s", Timer.toString(max));
68
+ UI.printInfo(Module.BENCH, " * Deviation: %s", Timer.toString(stdDev));
69
+ for (int i = 0; i < timers.length && timers[i] != null; i++) {
70
+ UI.printDetailed(Module.BENCH, " * Iteration %d: %s", i + 1, timers[i]);
71
+ }
72
+ }
73
+ }
@@ -0,0 +1,17 @@
1
+ package org.sunflow.system;
2
+
3
+ /**
4
+ * This interface is used to represent a piece of code which is to be
5
+ * benchmarked by repeatedly running and timing the kernel code. The begin/end
6
+ * routines are called per-iteration to do any local initialization which is not
7
+ * meant to be taken into acount in the timing (like preparing or destroying
8
+ * data structures).
9
+ */
10
+ public interface BenchmarkTest {
11
+
12
+ public void kernelBegin();
13
+
14
+ public void kernelMain();
15
+
16
+ public void kernelEnd();
17
+ }
@@ -0,0 +1,119 @@
1
+ package org.sunflow.system;
2
+
3
+ public class ByteUtil {
4
+
5
+ public static byte[] get2Bytes(int i) {
6
+ byte[] b = new byte[2];
7
+
8
+ b[0] = (byte) (i & 0xFF);
9
+ b[1] = (byte) ((i >> 8) & 0xFF);
10
+
11
+ return b;
12
+ }
13
+
14
+ public static byte[] get4Bytes(int i) {
15
+ byte[] b = new byte[4];
16
+
17
+ b[0] = (byte) (i & 0xFF);
18
+ b[1] = (byte) ((i >> 8) & 0xFF);
19
+ b[2] = (byte) ((i >> 16) & 0xFF);
20
+ b[3] = (byte) ((i >> 24) & 0xFF);
21
+
22
+ return b;
23
+ }
24
+
25
+ public static byte[] get4BytesInv(int i) {
26
+ byte[] b = new byte[4];
27
+
28
+ b[3] = (byte) (i & 0xFF);
29
+ b[2] = (byte) ((i >> 8) & 0xFF);
30
+ b[1] = (byte) ((i >> 16) & 0xFF);
31
+ b[0] = (byte) ((i >> 24) & 0xFF);
32
+
33
+ return b;
34
+ }
35
+
36
+ public static byte[] get8Bytes(long i) {
37
+ byte[] b = new byte[8];
38
+
39
+ b[0] = (byte) (i & 0xFF);
40
+ b[1] = (byte) ((i >> 8) & 0xFF);
41
+ b[2] = (byte) ((i >> 16) & 0xFF);
42
+ b[3] = (byte) ((i >> 24) & 0xFF);
43
+
44
+ b[4] = (byte) ((i >> 32) & 0xFF);
45
+ b[5] = (byte) ((i >> 40) & 0xFF);
46
+ b[6] = (byte) ((i >> 48) & 0xFF);
47
+ b[7] = (byte) ((i >> 56) & 0xFF);
48
+
49
+ return b;
50
+ }
51
+
52
+ public static long toLong(byte[] in) {
53
+ return (((toInt(in[0], in[1], in[2], in[3]))) | ((long) (toInt(in[4], in[5], in[6], in[7])) << (long) 32));
54
+ }
55
+
56
+ public static int toInt(byte in0, byte in1, byte in2, byte in3) {
57
+ return (in0 & 0xFF) | ((in1 & 0xFF) << 8) | ((in2 & 0xFF) << 16) | ((in3 & 0xFF) << 24);
58
+ }
59
+
60
+ public static int toInt(byte[] in) {
61
+ return toInt(in[0], in[1], in[2], in[3]);
62
+ }
63
+
64
+ public static int toInt(byte[] in, int ofs) {
65
+ return toInt(in[ofs + 0], in[ofs + 1], in[ofs + 2], in[ofs + 3]);
66
+ }
67
+
68
+ public static int floatToHalf(float f) {
69
+ int i = Float.floatToRawIntBits(f);
70
+ // unpack the s, e and m of the float
71
+ int s = (i >> 16) & 0x00008000;
72
+ int e = ((i >> 23) & 0x000000ff) - (127 - 15);
73
+ int m = i & 0x007fffff;
74
+ // pack them back up, forming a half
75
+ if (e <= 0) {
76
+ if (e < -10) {
77
+ // E is less than -10. The absolute value of f is less than
78
+ // HALF_MIN
79
+ // convert f to 0
80
+ return 0;
81
+ }
82
+ // E is between -10 and 0.
83
+ m = (m | 0x00800000) >> (1 - e);
84
+ // Round to nearest, round "0.5" up.
85
+ if ((m & 0x00001000) == 0x00001000) {
86
+ m += 0x00002000;
87
+ }
88
+ // Assemble the half from s, e (zero) and m.
89
+ return s | (m >> 13);
90
+ } else if (e == 0xff - (127 - 15)) {
91
+ if (m == 0) {
92
+ // F is an infinity; convert f to a half infinity
93
+ return s | 0x7c00;
94
+ } else {
95
+ // F is a NAN; we produce a half NAN that preserves the sign bit
96
+ // and the 10 leftmost bits of the significand of f
97
+ m >>= 13;
98
+ return s | 0x7c00 | m | ((m == 0) ? 0 : 1);
99
+ }
100
+ } else {
101
+ // E is greater than zero. F is a normalized float. Round to
102
+ // nearest, round "0.5" up
103
+ if ((m & 0x00001000) == 0x00001000) {
104
+ m += 0x00002000;
105
+ if ((m & 0x00800000) == 0x00800000) {
106
+ m = 0;
107
+ e += 1;
108
+ }
109
+ }
110
+ // Handle exponent overflow
111
+ if (e > 30) {
112
+ // overflow (); // Cause a hardware floating point overflow;
113
+ return s | 0x7c00; // if this returns, the half becomes an
114
+ } // infinity with the same sign as f.
115
+ // Assemble the half from s, e and m.
116
+ return s | (e << 10) | (m >> 13);
117
+ }
118
+ }
119
+ }