BLOB.C 73 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629
  1. /****************************************************************************
  2. * blob.c
  3. *
  4. * This module implements functions that manipulate blobs.
  5. *
  6. * The original file was written by Alexander Enzmann.
  7. * He wrote the code for blobs and generously provided us these enhancements.
  8. *
  9. * Modifications and enhancements by Dieter Bayer [DB].
  10. *
  11. * from Persistence of Vision(tm) Ray Tracer
  12. * Copyright 1996,1999 Persistence of Vision Team
  13. *---------------------------------------------------------------------------
  14. * NOTICE: This source code file is provided so that users may experiment
  15. * with enhancements to POV-Ray and to port the software to platforms other
  16. * than those supported by the POV-Ray Team. There are strict rules under
  17. * which you are permitted to use this file. The rules are in the file
  18. * named POVLEGAL.DOC which should be distributed with this file.
  19. * If POVLEGAL.DOC is not available or for more info please contact the POV-Ray
  20. * Team Coordinator by email to team-coord@povray.org or visit us on the web at
  21. * http://www.povray.org. The latest version of POV-Ray may be found at this site.
  22. *
  23. * This program is based on the popular DKB raytracer version 2.12.
  24. * DKBTrace was originally written by David K. Buck.
  25. * DKBTrace Ver 2.0-2.12 were written by David K. Buck & Aaron A. Collins.
  26. *
  27. *****************************************************************************/
  28. /****************************************************************************
  29. *
  30. * Explanation:
  31. *
  32. * -
  33. *
  34. * Syntax:
  35. *
  36. * blob
  37. * {
  38. * threshold THRESHOLD_VALUE
  39. *
  40. * component STRENGTH, RADIUS, <CENTER>
  41. *
  42. * sphere { <CENTER>, RADIUS, [strength] STRENGTH
  43. * [ translate <VECTOR> ]
  44. * [ rotate <VECTOR> ]
  45. * [ scale <VECTOR> ]
  46. * [ finish { ... } ]
  47. * [ pigment { ... } ]
  48. * [ tnormal { ... } ]
  49. * [ texture { ... } ]
  50. * }
  51. *
  52. * cylinder { <END1>, <END2>, RADIUS, [strength] STRENGTH
  53. * [ translate <VECTOR> ]
  54. * [ rotate <VECTOR> ]
  55. * [ scale <VECTOR> ]
  56. * [ finish { ... } ]
  57. * [ pigment { ... } ]
  58. * [ tnormal { ... } ]
  59. * [ texture { ... } ]
  60. * }
  61. *
  62. * [ sturm ]
  63. * [ hierarchy FLAG ]
  64. * }
  65. *
  66. * ---
  67. *
  68. * Jul 1994 : Most functions rewritten, bounding hierarchy added. [DB]
  69. *
  70. * Aug 1994 : Cylindrical blobs added. [DB]
  71. *
  72. * Sep 1994 : Multi-texturing added (each component can have its own texture).
  73. * Translation, rotation and scaling of each component added. [DB]
  74. *
  75. * Oct 1994 : Adopted the method for the bounding slab creation to build the
  76. * bounding sphere hierarchy of the blob to get a much better
  77. * hierarchy. Improved bounding sphere calculation for tighter
  78. * bounds. [DB]
  79. *
  80. * Dec 1994 : Added code for dynamic blob queue allocation. [DB]
  81. *
  82. * Feb 1995 : Moved bounding sphere stuff into a seperate file. [DB]
  83. *
  84. *****************************************************************************/
  85. #include "frame.h"
  86. #include "povray.h"
  87. #include "vector.h"
  88. #include "povproto.h"
  89. #include "blob.h"
  90. #include "bbox.h"
  91. #include "bsphere.h"
  92. #include "lighting.h"
  93. #include "matrices.h"
  94. #include "objects.h"
  95. #include "polysolv.h"
  96. #include "texture.h"
  97. /*****************************************************************************
  98. * Local preprocessor defines
  99. ******************************************************************************/
  100. /* Minimal intersection depth for a valid intersection. */
  101. #define DEPTH_TOLERANCE 1.0e-2
  102. /* Tolerance for inside test. */
  103. #define INSIDE_TOLERANCE 1.0e-6
  104. /* Ray enters/exits a component. */
  105. #define ENTERING 0
  106. #define EXITING 1
  107. /*****************************************************************************
  108. * Local typedefs
  109. ******************************************************************************/
  110. /*****************************************************************************
  111. * Static functions
  112. ******************************************************************************/
  113. static void element_normal (VECTOR Result, VECTOR P, BLOB_ELEMENT *Element);
  114. static int intersect_element (VECTOR P, VECTOR D, BLOB_ELEMENT *Element, DBL mindist, DBL *t0, DBL *t1);
  115. static void insert_hit (BLOB_ELEMENT *Element, DBL t0, DBL t1, BLOB_INTERVAL *intervals, int *cnt);
  116. static int determine_influences (VECTOR P, VECTOR D, BLOB *Blob, DBL mindist, BLOB_INTERVAL *intervals);
  117. static DBL calculate_field_value (BLOB *Blob, VECTOR P);
  118. static DBL calculate_element_field (BLOB_ELEMENT *Element, VECTOR P);
  119. static int intersect_cylinder (BLOB_ELEMENT *Element, VECTOR P, VECTOR D, DBL mindist, DBL *tmin, DBL *tmax);
  120. static int intersect_hemisphere (BLOB_ELEMENT *Element, VECTOR P, VECTOR D, DBL mindist, DBL *tmin, DBL *tmax);
  121. static int intersect_sphere (BLOB_ELEMENT *Element, VECTOR P, VECTOR D, DBL mindist, DBL *tmin, DBL *tmax);
  122. static int intersect_ellipsoid (BLOB_ELEMENT *Element, VECTOR P, VECTOR D, DBL mindist, DBL *tmin, DBL *tmax);
  123. static void get_element_bounding_sphere (BLOB_ELEMENT *Element, VECTOR Center, DBL *Radius2);
  124. static void build_bounding_hierarchy (BLOB *Blob);
  125. static void init_blob_element (BLOB_ELEMENT *Element);
  126. static void determine_element_texture (BLOB *Blob,
  127. BLOB_ELEMENT *Element, TEXTURE *Texture, VECTOR P, int *Count,
  128. TEXTURE **Textures, DBL *Weights);
  129. static void insert_node (BSPHERE_TREE *Node, int *size);
  130. static int All_Blob_Intersections (OBJECT *Object, RAY *Ray, ISTACK *Depth_Stack);
  131. static int Inside_Blob (VECTOR point, OBJECT *Object);
  132. static void Blob_Normal (VECTOR Result, OBJECT *Object, INTERSECTION *Inter);
  133. static BLOB *Copy_Blob (OBJECT *Object);
  134. static void Translate_Blob (OBJECT *Object, VECTOR Vector, TRANSFORM *Trans);
  135. static void Rotate_Blob (OBJECT *Object, VECTOR Vector, TRANSFORM *Trans);
  136. static void Scale_Blob (OBJECT *Object, VECTOR Vector, TRANSFORM *Trans);
  137. static void Invert_Blob (OBJECT *Object);
  138. static void Transform_Blob (OBJECT *Object, TRANSFORM *Trans);
  139. static void Destroy_Blob (OBJECT *Object);
  140. static void Compute_Blob_BBox (BLOB *Blob);
  141. /*****************************************************************************
  142. * Local variables
  143. ******************************************************************************/
  144. METHODS Blob_Methods =
  145. {
  146. All_Blob_Intersections,
  147. Inside_Blob, Blob_Normal,
  148. (COPY_METHOD)Copy_Blob,
  149. Translate_Blob, Rotate_Blob, Scale_Blob, Transform_Blob,
  150. Invert_Blob, Destroy_Blob
  151. };
  152. static BSPHERE_TREE **Queue;
  153. /* Maximum number of entries in queue during hierarchy traversal. */
  154. static unsigned Max_Queue_Size = 1024;
  155. /*****************************************************************************
  156. *
  157. * FUNCTION
  158. *
  159. * All_Blob_Intersections
  160. *
  161. * INPUT
  162. *
  163. * Object - Object
  164. * Ray - Ray
  165. *
  166. * OUTPUT
  167. *
  168. * Depth_Stack - Intersection stack
  169. *
  170. * RETURNS
  171. *
  172. * int - TRUE, if a intersection was found
  173. *
  174. * AUTHOR
  175. *
  176. * Alexander Enzmann
  177. *
  178. * DESCRIPTION
  179. *
  180. * Generate intervals of influence for each component. After these
  181. * are made, determine their aggregate effect on the ray. As the
  182. * individual intervals are checked, a quartic is generated
  183. * that represents the density at a particular point on the ray.
  184. *
  185. * Explanation for spherical components:
  186. *
  187. * After making the substitutions in MakeBlob, there is a formula
  188. * for each component that has the form:
  189. *
  190. * c0 * r^4 + c1 * r^2 + c2.
  191. *
  192. * In order to determine the influence on the ray of all of the
  193. * individual components, we start by determining the distance
  194. * from any point on the ray to the specified point. This can
  195. * be found using the pythagorean theorem, using C as the center
  196. * of this component, P as the start of the ray, and D as the
  197. * direction of travel of the ray:
  198. *
  199. * r^2 = (t * D + P - C) . (t * D + P - C)
  200. *
  201. * we insert this equation for each appearance of r^2 in the
  202. * components' formula, giving:
  203. *
  204. * r^2 = D.D t^2 + 2 t D . (P - C) + (P - C) . (P - C)
  205. *
  206. * Since the direction vector has been normalized, D.D = 1.
  207. * Using the substitutions:
  208. *
  209. * t0 = (P - C) . (P - C),
  210. * t1 = D . (P - C)
  211. *
  212. * We can write the formula as:
  213. *
  214. * r^2 = t0 + 2 t t1 + t^2
  215. *
  216. * Taking r^2 and substituting into the formula for this component
  217. * of the Blob we get the formula:
  218. *
  219. * density = c0 * (r^2)^2 + c1 * r^2 + c2,
  220. *
  221. * or:
  222. *
  223. * density = c0 * (t0 + 2 t t1 + t^2)^2 +
  224. * c1 * (t0 + 2 t t1 + t^2) +
  225. * c2
  226. *
  227. * Expanding terms and collecting with respect to "t" gives:
  228. *
  229. * t^4 * c0 +
  230. * t^3 * 4 c0 t1 +
  231. * t^2 * (c1 + 2 * c0 t0 + 4 c0 t1^2)
  232. * t * 2 (c1 t1 + 2 c0 t0 t1) +
  233. * c2 + c1*t0 + c0*t0^2
  234. *
  235. * This formula can now be solved for "t" by any of the quartic
  236. * root solvers that are available.
  237. *
  238. * CHANGES
  239. *
  240. * Jul 1994 : Added code for cylindrical and ellipsoidical blobs. [DB]
  241. *
  242. * Oct 1994 : Added code to convert polynomial into a bezier curve for
  243. * a quick test if an intersection exists in an interval. [DB]
  244. *
  245. * Sep 1995 : Added code to avoid numerical problems with distant blobs. [DB]
  246. *
  247. ******************************************************************************/
  248. static int All_Blob_Intersections(OBJECT *Object, RAY *Ray, ISTACK *Depth_Stack)
  249. {
  250. int i, j, cnt;
  251. int root_count, in_flag;
  252. int Intersection_Found = FALSE;
  253. DBL t0, t1, t2, c0, c1, c2;
  254. DBL dist, len, *fcoeffs, coeffs[5], roots[4];
  255. DBL start_dist;
  256. VECTOR P, D, V1, PP, DD;
  257. VECTOR IPoint;
  258. BLOB_INTERVAL *intervals;
  259. BLOB_ELEMENT *Element;
  260. BLOB *Blob = (BLOB *)Object;
  261. DBL l, w, newcoeffs[5], dk[5];
  262. Increase_Counter(stats[Ray_Blob_Tests]);
  263. /* Transform the ray into blob space. */
  264. if (Blob->Trans != NULL)
  265. {
  266. MInvTransPoint(P, Ray->Initial, Blob->Trans);
  267. MInvTransDirection(D, Ray->Direction, Blob->Trans);
  268. VLength(len, D);
  269. VInverseScaleEq(D, len);
  270. }
  271. else
  272. {
  273. Assign_Vector(P, Ray->Initial);
  274. Assign_Vector(D, Ray->Direction);
  275. len = 1.0;
  276. }
  277. /* Get the intervals along the ray where each component has an effect. */
  278. intervals = Blob->Data->Intervals;
  279. if ((cnt = determine_influences(P, D, Blob, DEPTH_TOLERANCE, intervals)) == 0)
  280. {
  281. /* Ray doesn't hit any of the component elements. */
  282. return (FALSE);
  283. }
  284. /* To avoid numerical problems we start at the first interval. */
  285. if ((start_dist = intervals[0].bound) < Small_Tolerance)
  286. {
  287. start_dist = 0.0;
  288. }
  289. for (i = 0; i < cnt; i++)
  290. {
  291. intervals[i].bound -= start_dist;
  292. }
  293. /* Get the new starting point. */
  294. VAddScaledEq(P, start_dist, D);
  295. /* Clear out the coefficients. */
  296. coeffs[0] =
  297. coeffs[1] =
  298. coeffs[2] =
  299. coeffs[3] = 0.0;
  300. coeffs[4] = - Blob->Data->Threshold;
  301. /*
  302. * Step through the list of intersection points, adding the
  303. * influence of each component as it appears.
  304. */
  305. fcoeffs = NULL;
  306. for (i = in_flag = 0; i < cnt; i++)
  307. {
  308. if ((intervals[i].type & 1) == ENTERING)
  309. {
  310. /*
  311. * Something is just starting to influence the ray, so calculate
  312. * its coefficients and add them into the pot.
  313. */
  314. in_flag++;
  315. Element = intervals[i].Element;
  316. switch (Element->Type)
  317. {
  318. case BLOB_SPHERE:
  319. VSub(V1, P, Element->O);
  320. VDot(t0, V1, V1);
  321. VDot(t1, V1, D);
  322. c0 = Element->c[0];
  323. c1 = Element->c[1];
  324. c2 = Element->c[2];
  325. fcoeffs = &(Element->f[0]);
  326. fcoeffs[0] = c0;
  327. fcoeffs[1] = 4.0 * c0 * t1;
  328. fcoeffs[2] = 2.0 * c0 * (2.0 * t1 * t1 + t0) + c1;
  329. fcoeffs[3] = 2.0 * t1 * (2.0 * c0 * t0 + c1);
  330. fcoeffs[4] = t0 * (c0 * t0 + c1) + c2;
  331. break;
  332. case BLOB_ELLIPSOID:
  333. MInvTransPoint(PP, P, Element->Trans);
  334. MInvTransDirection(DD, D, Element->Trans);
  335. VSub(V1, PP, Element->O);
  336. VDot(t0, V1, V1);
  337. VDot(t1, V1, DD);
  338. VDot(t2, DD, DD);
  339. c0 = Element->c[0];
  340. c1 = Element->c[1];
  341. c2 = Element->c[2];
  342. fcoeffs = &(Element->f[0]);
  343. fcoeffs[0] = c0 * t2 * t2;
  344. fcoeffs[1] = 4.0 * c0 * t1 * t2;
  345. fcoeffs[2] = 2.0 * c0 * (2.0 * t1 * t1 + t0 * t2) + c1 * t2;
  346. fcoeffs[3] = 2.0 * t1 * (2.0 * c0 * t0 + c1);
  347. fcoeffs[4] = t0 * (c0 * t0 + c1) + c2;
  348. break;
  349. case BLOB_BASE_HEMISPHERE:
  350. case BLOB_APEX_HEMISPHERE:
  351. MInvTransPoint(PP, P, Element->Trans);
  352. MInvTransDirection(DD, D, Element->Trans);
  353. if (Element->Type == BLOB_APEX_HEMISPHERE)
  354. {
  355. PP[Z] -= Element->len;
  356. }
  357. VDot(t0, PP, PP);
  358. VDot(t1, PP, DD);
  359. VDot(t2, DD, DD);
  360. c0 = Element->c[0];
  361. c1 = Element->c[1];
  362. c2 = Element->c[2];
  363. fcoeffs = &(Element->f[0]);
  364. fcoeffs[0] = c0 * t2 * t2;
  365. fcoeffs[1] = 4.0 * c0 * t1 * t2;
  366. fcoeffs[2] = 2.0 * c0 * (2.0 * t1 * t1 + t0 * t2) + c1 * t2;
  367. fcoeffs[3] = 2.0 * t1 * (2.0 * c0 * t0 + c1);
  368. fcoeffs[4] = t0 * (c0 * t0 + c1) + c2;
  369. break;
  370. case BLOB_CYLINDER:
  371. /* Transform ray into cylinder space. */
  372. MInvTransPoint(PP, P, Element->Trans);
  373. MInvTransDirection(DD, D, Element->Trans);
  374. t0 = PP[X] * PP[X] + PP[Y] * PP[Y];
  375. t1 = PP[X] * DD[X] + PP[Y] * DD[Y];
  376. t2 = DD[X] * DD[X] + DD[Y] * DD[Y];
  377. c0 = Element->c[0];
  378. c1 = Element->c[1];
  379. c2 = Element->c[2];
  380. fcoeffs = &(Element->f[0]);
  381. fcoeffs[0] = c0 * t2 * t2;
  382. fcoeffs[1] = 4.0 * c0 * t1 * t2;
  383. fcoeffs[2] = 2.0 * c0 * (2.0 * t1 * t1 + t0 * t2) + c1 * t2;
  384. fcoeffs[3] = 2.0 * t1 * (2.0 * c0 * t0 + c1);
  385. fcoeffs[4] = t0 * (c0 * t0 + c1) + c2;
  386. break;
  387. default:
  388. Error("Unknown blob component in All_Blob_Intersections().\n");
  389. }
  390. for (j = 0; j < 5; j++)
  391. {
  392. coeffs[j] += fcoeffs[j];
  393. }
  394. }
  395. else
  396. {
  397. /*
  398. * We are losing the influence of a component -->
  399. * subtract off its coefficients.
  400. */
  401. fcoeffs = intervals[i].Element->f;
  402. for (j = 0; j < 5; j++)
  403. {
  404. coeffs[j] -= fcoeffs[j];
  405. }
  406. /* If no components are currently affecting the ray ---> skip ahead. */
  407. if (--in_flag == 0)
  408. {
  409. continue;
  410. }
  411. }
  412. /*
  413. * If the following intersection lies close to the current intersection
  414. * then first add/subtract next region before testing. [DB 7/94]
  415. */
  416. if ((i + 1 < cnt) && (fabs(intervals[i].bound - intervals[i + 1].bound) < EPSILON))
  417. {
  418. continue;
  419. }
  420. /*
  421. * Transform polynomial in a way that the interval boundaries are moved
  422. * to 0 and 1, i. e. the roots of interest are between 0 and 1. [DB 10/94]
  423. */
  424. l = intervals[i].bound;
  425. w = intervals[i+1].bound - l;
  426. newcoeffs[0] = coeffs[0] * w * w * w * w;
  427. newcoeffs[1] = (coeffs[1] + 4.0 * coeffs[0] * l) * w * w * w;
  428. newcoeffs[2] = (3.0 * l * (2.0 * coeffs[0] * l + coeffs[1]) + coeffs[2]) * w * w;
  429. newcoeffs[3] = (2.0 * l * (2.0 * l * (coeffs[0] * l + 0.75 * coeffs[1]) + coeffs[2]) + coeffs[3]) * w;
  430. newcoeffs[4] = l * (l * (l * (coeffs[0] * l + coeffs[1]) + coeffs[2]) + coeffs[3]) + coeffs[4];
  431. /* Calculate coefficients of corresponding bezier curve. [DB 10/94] */
  432. dk[0] = newcoeffs[4];
  433. dk[1] = newcoeffs[4] + 0.25 * newcoeffs[3];
  434. dk[2] = newcoeffs[4] + 0.50 * (newcoeffs[3] + newcoeffs[2] / 12.0);
  435. dk[3] = newcoeffs[4] + 0.50 * (0.375 * newcoeffs[3] + newcoeffs[2] + 0.125 * newcoeffs[1]);
  436. dk[4] = newcoeffs[4] + newcoeffs[3] + newcoeffs[2] + newcoeffs[1] + newcoeffs[0];
  437. /*
  438. * Skip this interval if the ray doesn't intersect the convex hull of the
  439. * bezier curve, because no valid intersection will be found. [DB 10/94]
  440. */
  441. if (((dk[0] >= 0.0) && (dk[1] >= 0.0) && (dk[2] >= 0.0) && (dk[3] >= 0.0) && (dk[4] >= 0.0)) ||
  442. ((dk[0] <= 0.0) && (dk[1] <= 0.0) && (dk[2] <= 0.0) && (dk[3] <= 0.0) && (dk[4] <= 0.0)))
  443. {
  444. continue;
  445. }
  446. /*
  447. * Now we could do bezier clipping to find the roots
  448. * but I have no idea how this works. [DB 2/95]
  449. */
  450. /* Solve polynomial. */
  451. root_count = Solve_Polynomial(4, coeffs, roots, Test_Flag(Blob, STURM_FLAG), 1.0e-11);
  452. /* See if any of the roots are valid. */
  453. for (j = 0; j < root_count; j++)
  454. {
  455. dist = roots[j];
  456. /*
  457. * First see if the root is in the interval of influence of
  458. * the currently active components.
  459. */
  460. if ((dist >= intervals[i].bound) &&
  461. (dist <= intervals[i+1].bound))
  462. {
  463. /* Correct distance. */
  464. dist = (dist + start_dist) / len;
  465. if ((dist > DEPTH_TOLERANCE) && (dist < Max_Distance))
  466. {
  467. VEvaluateRay(IPoint, Ray->Initial, dist, Ray->Direction);
  468. if (Point_In_Clip(IPoint, Object->Clip))
  469. {
  470. push_entry(dist, IPoint, Object, Depth_Stack);
  471. Intersection_Found = TRUE;
  472. }
  473. }
  474. }
  475. }
  476. /*
  477. * If the blob isn't used inside a CSG and we have found at least
  478. * one intersection then we are ready, because all possible intersections
  479. * will be further away (we have a sorted list!). [DB 7/94]
  480. */
  481. if (!(Blob->Type & IS_CHILD_OBJECT) && (Intersection_Found))
  482. {
  483. break;
  484. }
  485. }
  486. if (Intersection_Found)
  487. {
  488. Increase_Counter(stats[Ray_Blob_Tests_Succeeded]);
  489. }
  490. return (Intersection_Found);
  491. }
  492. /*****************************************************************************
  493. *
  494. * FUNCTION
  495. *
  496. * insert_hit
  497. *
  498. * INPUT
  499. *
  500. * Blob - Pointer to blob structure
  501. * Element - Element to insert
  502. * t0, t1 - Intersection depths
  503. *
  504. * OUTPUT
  505. *
  506. * intervals - Pointer to sorted list of hits
  507. * cnt - Number of hits in intervals
  508. *
  509. * RETURNS
  510. *
  511. * AUTHOR
  512. *
  513. * Alexander Enzmann
  514. *
  515. * DESCRIPTION
  516. *
  517. * Store the points of intersection. Keep track of: whether this is
  518. * the start or end point of the hit, which component was pierced
  519. * by the ray, and the point along the ray that the hit occured at.
  520. *
  521. * CHANGES
  522. *
  523. * Oct 1994 : Modified to use memmove instead of loops for copying. [DB]
  524. * Sep 1995 : Changed to allow use of memcpy if memmove isn't available. [AED]
  525. * Jul 1996 : Changed to use POV_MEMMOVE, which can be memmove or pov_memmove.
  526. * Oct 1996 : Changed to avoid unnecessary compares. [DB]
  527. *
  528. ******************************************************************************/
  529. static void insert_hit(BLOB_ELEMENT *Element, DBL t0, DBL t1, BLOB_INTERVAL *intervals, int *cnt)
  530. {
  531. int k;
  532. /* We are entering the component. */
  533. intervals[*cnt].type = Element->Type | ENTERING;
  534. intervals[*cnt].bound = t0;
  535. intervals[*cnt].Element = Element;
  536. for (k = 0; t0 > intervals[k].bound; k++);
  537. if (k < *cnt)
  538. {
  539. /*
  540. * This hit point is smaller than one that already exists -->
  541. * bump the rest and insert it here.
  542. */
  543. POV_MEMMOVE(&intervals[k+1], &intervals[k], (*cnt-k)*sizeof(BLOB_INTERVAL));
  544. /* We are entering the component. */
  545. intervals[k].type = Element->Type | ENTERING;
  546. intervals[k].bound = t0;
  547. intervals[k].Element = Element;
  548. (*cnt)++;
  549. /* We are exiting the component. */
  550. intervals[*cnt].type = Element->Type | EXITING;
  551. intervals[*cnt].bound = t1;
  552. intervals[*cnt].Element = Element;
  553. for (k = k + 1; t1 > intervals[k].bound; k++);
  554. if (k < *cnt)
  555. {
  556. POV_MEMMOVE(&intervals[k+1], &intervals[k], (*cnt-k)*sizeof(BLOB_INTERVAL));
  557. /* We are exiting the component. */
  558. intervals[k].type = Element->Type | EXITING;
  559. intervals[k].bound = t1;
  560. intervals[k].Element = Element;
  561. }
  562. (*cnt)++;
  563. }
  564. else
  565. {
  566. /* Just plop the start and end points at the end of the list */
  567. (*cnt)++;
  568. /* We are exiting the component. */
  569. intervals[*cnt].type = Element->Type | EXITING;
  570. intervals[*cnt].bound = t1;
  571. intervals[*cnt].Element = Element;
  572. (*cnt)++;
  573. }
  574. }
  575. /*****************************************************************************
  576. *
  577. * FUNCTION
  578. *
  579. * intersect_cylinder
  580. *
  581. * INPUT
  582. *
  583. * Element - Pointer to element structure
  584. * P, D - Ray = P + t * D
  585. * mindist - Min. valid distance
  586. *
  587. * OUTPUT
  588. *
  589. * tmin, tmax - Intersection depths found
  590. *
  591. * RETURNS
  592. *
  593. * AUTHOR
  594. *
  595. * Dieter Bayer (with help from Alexander Enzmann)
  596. *
  597. * DESCRIPTION
  598. *
  599. * -
  600. *
  601. * CHANGES
  602. *
  603. * Jul 1994 : Creation.
  604. *
  605. ******************************************************************************/
  606. static int intersect_cylinder(BLOB_ELEMENT *Element, VECTOR P, VECTOR D, DBL mindist, DBL *tmin, DBL *tmax)
  607. {
  608. DBL a, b, c, d, t, u, v, w, len;
  609. VECTOR PP, DD;
  610. /* Transform ray into cylinder space. */
  611. MInvTransPoint(PP, P, Element->Trans);
  612. MInvTransDirection(DD, D, Element->Trans);
  613. VLength(len, DD);
  614. VInverseScaleEq(DD, len);
  615. /* Intersect ray with cylinder. */
  616. a = DD[X] * DD[X] + DD[Y] * DD[Y];
  617. if (a > EPSILON)
  618. {
  619. b = PP[X] * DD[X] + PP[Y] * DD[Y];
  620. c = PP[X] * PP[X] + PP[Y] * PP[Y] - Element->rad2;
  621. d = b * b - a * c;
  622. if (d > EPSILON)
  623. {
  624. d = sqrt(d);
  625. t = ( - b + d) / a;
  626. w = PP[Z] + t * DD[Z];
  627. if ((w >= 0.0) && (w <= Element->len))
  628. {
  629. if (t < *tmin) { *tmin = t; }
  630. if (t > *tmax) { *tmax = t; }
  631. }
  632. t = ( - b - d) / a;
  633. w = PP[Z] + t * DD[Z];
  634. if ((w >= 0.0) && (w <= Element->len))
  635. {
  636. if (t < *tmin) { *tmin = t; }
  637. if (t > *tmax) { *tmax = t; }
  638. }
  639. }
  640. }
  641. /* Intersect base/cap plane. */
  642. if (fabs(DD[Z]) > EPSILON)
  643. {
  644. /* Intersect base plane. */
  645. t = - PP[Z] / DD[Z];
  646. u = PP[X] + t * DD[X];
  647. v = PP[Y] + t * DD[Y];
  648. if ((u * u + v * v) <= Element->rad2)
  649. {
  650. if (t < *tmin) { *tmin = t; }
  651. if (t > *tmax) { *tmax = t; }
  652. }
  653. /* Intersect cap plane. */
  654. t = (Element->len - PP[Z]) / DD[Z];
  655. u = PP[X] + t * DD[X];
  656. v = PP[Y] + t * DD[Y];
  657. if ((u * u + v * v) <= Element->rad2)
  658. {
  659. if (t < *tmin) { *tmin = t; }
  660. if (t > *tmax) { *tmax = t; }
  661. }
  662. }
  663. /* Check if the intersections are valid. */
  664. *tmin /= len;
  665. *tmax /= len;
  666. if (*tmin < mindist) { *tmin = 0.0; }
  667. if (*tmax < mindist) { *tmax = 0.0; }
  668. if (*tmin >= *tmax)
  669. {
  670. return (FALSE);
  671. }
  672. return (TRUE);
  673. }
  674. /*****************************************************************************
  675. *
  676. * FUNCTION
  677. *
  678. * intersect_ellipsoid
  679. *
  680. * INPUT
  681. *
  682. * Element - Pointer to element structure
  683. * P, D - Ray = P + t * D
  684. * mindist - Min. valid distance
  685. *
  686. * OUTPUT
  687. *
  688. * tmin, tmax - Intersection depths found
  689. *
  690. * RETURNS
  691. *
  692. * AUTHOR
  693. *
  694. * Dieter Bayer
  695. *
  696. * DESCRIPTION
  697. *
  698. * -
  699. *
  700. * CHANGES
  701. *
  702. * Sep 1994 : Creation.
  703. *
  704. ******************************************************************************/
  705. static int intersect_ellipsoid(BLOB_ELEMENT *Element, VECTOR P, VECTOR D, DBL mindist, DBL *tmin, DBL *tmax)
  706. {
  707. DBL b, d, t, len;
  708. VECTOR V1, PP, DD;
  709. MInvTransPoint(PP, P, Element->Trans);
  710. MInvTransDirection(DD, D, Element->Trans);
  711. VLength(len, DD);
  712. VInverseScaleEq(DD, len);
  713. VSub(V1, PP, Element->O);
  714. VDot(b, V1, DD);
  715. VDot(t, V1, V1);
  716. d = b * b - t + Element->rad2;
  717. if (d < EPSILON)
  718. {
  719. return (FALSE);
  720. }
  721. d = sqrt(d);
  722. *tmax = ( - b + d) / len; if (*tmax < mindist) { *tmax = 0.0; }
  723. *tmin = ( - b - d) / len; if (*tmin < mindist) { *tmin = 0.0; }
  724. if (*tmax == *tmin)
  725. {
  726. return (FALSE);
  727. }
  728. else
  729. {
  730. if (*tmax < *tmin)
  731. {
  732. d = *tmin; *tmin = *tmax; *tmax = d;
  733. }
  734. }
  735. return (TRUE);
  736. }
  737. /*****************************************************************************
  738. *
  739. * FUNCTION
  740. *
  741. * intersect_hemisphere
  742. *
  743. * INPUT
  744. *
  745. * Element - Pointer to element structure
  746. * P, D - Ray = P + t * D
  747. * mindist - Min. valid distance
  748. *
  749. * OUTPUT
  750. *
  751. * tmin, tmax - Intersection depths found
  752. *
  753. * RETURNS
  754. *
  755. * AUTHOR
  756. *
  757. * Dieter Bayer
  758. *
  759. * DESCRIPTION
  760. *
  761. * -
  762. *
  763. * CHANGES
  764. *
  765. * Jul 1994 : Creation (with help from Alexander Enzmann).
  766. *
  767. ******************************************************************************/
  768. static int intersect_hemisphere(BLOB_ELEMENT *Element, VECTOR P, VECTOR D, DBL mindist, DBL *tmin, DBL *tmax)
  769. {
  770. DBL b, d, t, z1, z2, len;
  771. VECTOR PP, DD;
  772. /* Transform ray into hemisphere space. */
  773. MInvTransPoint(PP, P, Element->Trans);
  774. MInvTransDirection(DD, D, Element->Trans);
  775. VLength(len, DD);
  776. VInverseScaleEq(DD, len);
  777. if (Element->Type == BLOB_BASE_HEMISPHERE)
  778. {
  779. VDot(b, PP, DD);
  780. VDot(t, PP, PP);
  781. d = b * b - t + Element->rad2;
  782. if (d < EPSILON)
  783. {
  784. return (FALSE);
  785. }
  786. d = sqrt(d);
  787. *tmax = - b + d;
  788. *tmin = - b - d;
  789. if (*tmax < *tmin)
  790. {
  791. d = *tmin; *tmin = *tmax; *tmax = d;
  792. }
  793. /* Cut intersection at the plane. */
  794. z1 = PP[Z] + *tmin * DD[Z];
  795. z2 = PP[Z] + *tmax * DD[Z];
  796. /* If both points are inside --> no intersection */
  797. if ((z1 >= 0.0) && (z2 >= 0.0))
  798. {
  799. return (FALSE);
  800. }
  801. /* If both points are outside --> intersections found */
  802. if ((z1 < 0.0) && (z2 < 0.0))
  803. {
  804. *tmin /= len;
  805. *tmax /= len;
  806. return (TRUE);
  807. }
  808. /* Determine intersection with plane. */
  809. t = - PP[Z] / DD[Z];
  810. if (z1 >= 0.0)
  811. {
  812. /* Ray is crossing the plane from inside to outside. */
  813. *tmin = (t < mindist) ? 0.0 : t;
  814. }
  815. else
  816. {
  817. /* Ray is crossing the plane from outside to inside. */
  818. *tmax = (t < mindist) ? 0.0 : t;
  819. }
  820. *tmin /= len;
  821. *tmax /= len;
  822. return (TRUE);
  823. }
  824. else
  825. {
  826. PP[Z] -= Element->len;
  827. VDot(b, PP, DD);
  828. VDot(t, PP, PP);
  829. d = b * b - t + Element->rad2;
  830. if (d < EPSILON)
  831. {
  832. return (FALSE);
  833. }
  834. d = sqrt(d);
  835. *tmax = - b + d;
  836. *tmin = - b - d;
  837. if (*tmax < *tmin)
  838. {
  839. d = *tmin; *tmin = *tmax; *tmax = d;
  840. }
  841. /* Cut intersection at the plane. */
  842. z1 = PP[Z] + *tmin * DD[Z];
  843. z2 = PP[Z] + *tmax * DD[Z];
  844. /* If both points are inside --> no intersection */
  845. if ((z1 <= 0.0) && (z2 <= 0.0))
  846. {
  847. return (FALSE);
  848. }
  849. /* If both points are outside --> intersections found */
  850. if ((z1 > 0.0) && (z2 > 0.0))
  851. {
  852. *tmin /= len;
  853. *tmax /= len;
  854. return (TRUE);
  855. }
  856. /* Determine intersection with plane. */
  857. t = - PP[Z] / DD[Z];
  858. if (z1 <= 0.0)
  859. {
  860. /* Ray is crossing the plane from inside to outside. */
  861. *tmin = (t < mindist) ? 0.0 : t;
  862. }
  863. else
  864. {
  865. /* Ray is crossing the plane from outside to inside. */
  866. *tmax = (t < mindist) ? 0.0 : t;
  867. }
  868. *tmin /= len;
  869. *tmax /= len;
  870. return (TRUE);
  871. }
  872. }
  873. /*****************************************************************************
  874. *
  875. * FUNCTION
  876. *
  877. * intersect_sphere
  878. *
  879. * INPUT
  880. *
  881. * Element - Pointer to element structure
  882. * P, D - Ray = P + t * D
  883. * mindist - Min. valid distance
  884. *
  885. * OUTPUT
  886. *
  887. * tmin, tmax - Intersection depths found
  888. *
  889. * RETURNS
  890. *
  891. * AUTHOR
  892. *
  893. * Dieter Bayer
  894. *
  895. * DESCRIPTION
  896. *
  897. * -
  898. *
  899. * CHANGES
  900. *
  901. * Jul 1994 : Creation (with help from Alexander Enzmann).
  902. *
  903. ******************************************************************************/
  904. static int intersect_sphere(BLOB_ELEMENT *Element, VECTOR P, VECTOR D, DBL mindist, DBL *tmin, DBL *tmax)
  905. {
  906. DBL b, d, t;
  907. VECTOR V1;
  908. VSub(V1, P, Element->O);
  909. VDot(b, V1, D);
  910. VDot(t, V1, V1);
  911. d = b * b - t + Element->rad2;
  912. if (d < EPSILON)
  913. {
  914. return (FALSE);
  915. }
  916. d = sqrt(d);
  917. *tmax = - b + d; if (*tmax < mindist) { *tmax = 0.0; }
  918. *tmin = - b - d; if (*tmin < mindist) { *tmin = 0.0; }
  919. if (*tmax == *tmin)
  920. {
  921. return (FALSE);
  922. }
  923. else
  924. {
  925. if (*tmax < *tmin)
  926. {
  927. d = *tmin; *tmin = *tmax; *tmax = d;
  928. }
  929. }
  930. return (TRUE);
  931. }
  932. /*****************************************************************************
  933. *
  934. * FUNCTION
  935. *
  936. * intersect_element
  937. *
  938. * INPUT
  939. *
  940. * P, D - Ray = P + t * D
  941. * Element - Pointer to element structure
  942. * mindist - Min. valid distance
  943. *
  944. * OUTPUT
  945. *
  946. * tmin, tmax - Intersection depths found
  947. *
  948. * RETURNS
  949. *
  950. * AUTHOR
  951. *
  952. * Dieter Bayer
  953. *
  954. * DESCRIPTION
  955. *
  956. * -
  957. *
  958. * CHANGES
  959. *
  960. * Jul 1994 : Creation.
  961. *
  962. ******************************************************************************/
  963. static int intersect_element(VECTOR P, VECTOR D, BLOB_ELEMENT *Element, DBL mindist, DBL *tmin, DBL *tmax)
  964. {
  965. #ifdef BLOB_EXTRA_STATS
  966. Increase_Counter(stats[Blob_Element_Tests]);
  967. #endif
  968. *tmin = BOUND_HUGE;
  969. *tmax = - BOUND_HUGE;
  970. switch (Element->Type)
  971. {
  972. case BLOB_SPHERE:
  973. if (!intersect_sphere(Element, P, D, mindist, tmin, tmax))
  974. {
  975. return (FALSE);
  976. }
  977. break;
  978. case BLOB_ELLIPSOID:
  979. if (!intersect_ellipsoid(Element, P, D, mindist, tmin, tmax))
  980. {
  981. return (FALSE);
  982. }
  983. break;
  984. case BLOB_BASE_HEMISPHERE:
  985. case BLOB_APEX_HEMISPHERE:
  986. if (!intersect_hemisphere(Element, P, D, mindist, tmin, tmax))
  987. {
  988. return (FALSE);
  989. }
  990. break;
  991. case BLOB_CYLINDER:
  992. if (!intersect_cylinder(Element, P, D, mindist, tmin, tmax))
  993. {
  994. return (FALSE);
  995. }
  996. break;
  997. }
  998. #ifdef BLOB_EXTRA_STATS
  999. Increase_Counter(stats[Blob_Element_Tests_Succeeded]);
  1000. #endif
  1001. return (TRUE);
  1002. }
  1003. /*****************************************************************************
  1004. *
  1005. * FUNCTION
  1006. *
  1007. * determine_influences
  1008. *
  1009. * INPUT
  1010. *
  1011. * P, D - Ray = P + t * D
  1012. * Blob - Pointer to blob structure
  1013. * mindist - Min. valid distance
  1014. *
  1015. * OUTPUT
  1016. *
  1017. * intervals - Sorted list of intersections found
  1018. *
  1019. * RETURNS
  1020. *
  1021. * int - Number of intersection found
  1022. *
  1023. * AUTHOR
  1024. *
  1025. * Dieter Bayer
  1026. *
  1027. * DESCRIPTION
  1028. *
  1029. * Make a sorted list of points along the ray at which the various blob
  1030. * components start and stop adding their influence.
  1031. *
  1032. * CHANGES
  1033. *
  1034. * Jul 1994 : Added code for bounding hierarchy traversal. [DB]
  1035. *
  1036. ******************************************************************************/
  1037. static int determine_influences(VECTOR P, VECTOR D, BLOB *Blob, DBL mindist, BLOB_INTERVAL *intervals)
  1038. {
  1039. int i;
  1040. int cnt, size;
  1041. DBL b, t, t0, t1;
  1042. VECTOR V1;
  1043. BSPHERE_TREE *Tree;
  1044. cnt = 0;
  1045. if (Blob->Data->Tree == NULL)
  1046. {
  1047. /* There's no bounding hierarchy so just step through all elements. */
  1048. for (i = 0; i < Blob->Data->Number_Of_Components; i++)
  1049. {
  1050. if (intersect_element(P, D, &Blob->Data->Entry[i], mindist, &t0, &t1))
  1051. {
  1052. insert_hit(&Blob->Data->Entry[i], t0, t1, intervals, &cnt);
  1053. }
  1054. }
  1055. }
  1056. else
  1057. {
  1058. /* Use blob's bounding hierarchy. */
  1059. size = 0;
  1060. Queue[size++] = Blob->Data->Tree;
  1061. while (size > 0)
  1062. {
  1063. Tree = Queue[--size];
  1064. /* Test if current node is a leaf. */
  1065. if (Tree->Entries <= 0)
  1066. {
  1067. /* Test element. */
  1068. if (intersect_element(P, D, (BLOB_ELEMENT *)Tree->Node, mindist, &t0, &t1))
  1069. {
  1070. insert_hit((BLOB_ELEMENT *)Tree->Node, t0, t1, intervals, &cnt);
  1071. }
  1072. }
  1073. else
  1074. {
  1075. /* Test all sub-nodes. */
  1076. for (i = 0; i < (int)Tree->Entries; i++)
  1077. {
  1078. #ifdef BLOB_EXTRA_STATS
  1079. Increase_Counter(stats[Blob_Bound_Tests]);
  1080. #endif
  1081. VSub(V1, Tree->Node[i]->C, P);
  1082. VDot(b, V1, D);
  1083. VDot(t, V1, V1);
  1084. if ((t - Sqr(b)) <= Tree->Node[i]->r2)
  1085. {
  1086. #ifdef BLOB_EXTRA_STATS
  1087. Increase_Counter(stats[Blob_Bound_Tests_Succeeded]);
  1088. #endif
  1089. insert_node(Tree->Node[i], &size);
  1090. }
  1091. }
  1092. }
  1093. }
  1094. }
  1095. return (cnt);
  1096. }
  1097. /*****************************************************************************
  1098. *
  1099. * FUNCTION
  1100. *
  1101. * calculate_element_field
  1102. *
  1103. * INPUT
  1104. *
  1105. * Element - Pointer to element structure
  1106. * P - Point whos field value is calculated
  1107. *
  1108. * OUTPUT
  1109. *
  1110. * RETURNS
  1111. *
  1112. * DBL - Field value
  1113. *
  1114. * AUTHOR
  1115. *
  1116. * Alexander Enzmann
  1117. *
  1118. * DESCRIPTION
  1119. *
  1120. * Calculate the field value of a single element in a given point P
  1121. * (which must already have been transformed into blob space).
  1122. *
  1123. * CHANGES
  1124. *
  1125. * Jul 1994 : Added code for cylindrical and ellipsoidical blobs. [DB]
  1126. *
  1127. ******************************************************************************/
  1128. static DBL calculate_element_field(BLOB_ELEMENT *Element, VECTOR P)
  1129. {
  1130. DBL rad2, density;
  1131. VECTOR V1, PP;
  1132. density = 0.0;
  1133. switch (Element->Type)
  1134. {
  1135. case BLOB_SPHERE:
  1136. VSub(V1, P, Element->O);
  1137. VDot(rad2, V1, V1);
  1138. if (rad2 < Element->rad2)
  1139. {
  1140. density = rad2 * (rad2 * Element->c[0] + Element->c[1]) + Element->c[2];
  1141. }
  1142. break;
  1143. case BLOB_ELLIPSOID:
  1144. MInvTransPoint(PP, P, Element->Trans);
  1145. VSub(V1, PP, Element->O);
  1146. VDot(rad2, V1, V1);
  1147. if (rad2 < Element->rad2)
  1148. {
  1149. density = rad2 * (rad2 * Element->c[0] + Element->c[1]) + Element->c[2];
  1150. }
  1151. break;
  1152. case BLOB_BASE_HEMISPHERE:
  1153. MInvTransPoint(PP, P, Element->Trans);
  1154. if (PP[Z] <= 0.0)
  1155. {
  1156. VDot(rad2, PP, PP);
  1157. if (rad2 <= Element->rad2)
  1158. {
  1159. density = rad2 * (rad2 * Element->c[0] + Element->c[1]) + Element->c[2];
  1160. }
  1161. }
  1162. break;
  1163. case BLOB_APEX_HEMISPHERE:
  1164. MInvTransPoint(PP, P, Element->Trans);
  1165. PP[Z] -= Element->len;
  1166. if (PP[Z] >= 0.0)
  1167. {
  1168. VDot(rad2, PP, PP);
  1169. if (rad2 <= Element->rad2)
  1170. {
  1171. density = rad2 * (rad2 * Element->c[0] + Element->c[1]) + Element->c[2];
  1172. }
  1173. }
  1174. break;
  1175. case BLOB_CYLINDER:
  1176. MInvTransPoint(PP, P, Element->Trans);
  1177. if ((PP[Z] >= 0.0) && (PP[Z] <= Element->len))
  1178. {
  1179. if ((rad2 = Sqr(PP[X]) + Sqr(PP[Y])) <= Element->rad2)
  1180. {
  1181. density = rad2 * (rad2 * Element->c[0] + Element->c[1]) + Element->c[2];
  1182. }
  1183. }
  1184. break;
  1185. }
  1186. return (density);
  1187. }
  1188. /*****************************************************************************
  1189. *
  1190. * FUNCTION
  1191. *
  1192. * calculate_field_value
  1193. *
  1194. * INPUT
  1195. *
  1196. * Blob - Pointer to blob structure
  1197. * P - Point whos field value is calculated
  1198. *
  1199. * OUTPUT
  1200. *
  1201. * RETURNS
  1202. *
  1203. * DBL - Field value
  1204. *
  1205. * AUTHOR
  1206. *
  1207. * Dieter Bayer
  1208. *
  1209. * DESCRIPTION
  1210. *
  1211. * Calculate the field value of a blob in a given point P
  1212. * (which must already have been transformed into blob space).
  1213. *
  1214. * CHANGES
  1215. *
  1216. * Jul 1994 : Added code for bounding hierarchy traversal. [DB]
  1217. *
  1218. ******************************************************************************/
  1219. static DBL calculate_field_value(BLOB *Blob, VECTOR P)
  1220. {
  1221. int i;
  1222. int size;
  1223. DBL density, rad2;
  1224. VECTOR V1;
  1225. BSPHERE_TREE *Tree;
  1226. density = 0.0;
  1227. if (Blob->Data->Tree == NULL)
  1228. {
  1229. /* There's no tree --> step through all elements. */
  1230. for (i = 0; i < Blob->Data->Number_Of_Components; i++)
  1231. {
  1232. density += calculate_element_field(&Blob->Data->Entry[i], P);
  1233. }
  1234. }
  1235. else
  1236. {
  1237. /* A tree exists --> step through the tree. */
  1238. size = 0;
  1239. Queue[size++] = Blob->Data->Tree;
  1240. while (size > 0)
  1241. {
  1242. Tree = Queue[--size];
  1243. /* Test if current node is a leaf. */
  1244. if (Tree->Entries <= 0)
  1245. {
  1246. density += calculate_element_field((BLOB_ELEMENT *)Tree->Node, P);
  1247. }
  1248. else
  1249. {
  1250. /* Test all sub-nodes. */
  1251. for (i = 0; i < (int)Tree->Entries; i++)
  1252. {
  1253. /* Insert sub-node if we are inside. */
  1254. VSub(V1, P, Tree->Node[i]->C);
  1255. VDot(rad2, V1, V1);
  1256. if (rad2 <= Tree->Node[i]->r2)
  1257. {
  1258. insert_node(Tree->Node[i], &size);
  1259. }
  1260. }
  1261. }
  1262. }
  1263. }
  1264. return (density);
  1265. }
  1266. /*****************************************************************************
  1267. *
  1268. * FUNCTION
  1269. *
  1270. * Inside_Blob
  1271. *
  1272. * INPUT
  1273. *
  1274. * Test_Point - Point to test
  1275. * Object - Pointer to blob structure
  1276. *
  1277. * OUTPUT
  1278. *
  1279. * RETURNS
  1280. *
  1281. * int - TRUE if Test_Point is inside
  1282. *
  1283. * AUTHOR
  1284. *
  1285. * Alexander Enzmann
  1286. *
  1287. * DESCRIPTION
  1288. *
  1289. * Calculate the density at the given point and then compare to
  1290. * the threshold to see if we are in or out of the blob.
  1291. *
  1292. * CHANGES
  1293. *
  1294. * -
  1295. *
  1296. ******************************************************************************/
  1297. static int Inside_Blob(VECTOR Test_Point, OBJECT *Object)
  1298. {
  1299. VECTOR New_Point;
  1300. BLOB *Blob = (BLOB *) Object;
  1301. /* Transform the point into blob space. */
  1302. if (Blob->Trans != NULL)
  1303. {
  1304. MInvTransPoint(New_Point, Test_Point, Blob->Trans);
  1305. }
  1306. else
  1307. {
  1308. Assign_Vector(New_Point, Test_Point);
  1309. }
  1310. if (calculate_field_value(Blob, New_Point) > Blob->Data->Threshold - INSIDE_TOLERANCE)
  1311. {
  1312. /* We are inside. */
  1313. return (!Test_Flag(Blob, INVERTED_FLAG));
  1314. }
  1315. else
  1316. {
  1317. /* We are outside. */
  1318. return (Test_Flag(Blob, INVERTED_FLAG));
  1319. }
  1320. }
  1321. /*****************************************************************************
  1322. *
  1323. * FUNCTION
  1324. *
  1325. * element_normal
  1326. *
  1327. * INPUT
  1328. *
  1329. * P - Surface point
  1330. * Element - Pointer to element structure
  1331. *
  1332. * OUTPUT
  1333. *
  1334. * Result - Element's normal
  1335. *
  1336. * RETURNS
  1337. *
  1338. * AUTHOR
  1339. *
  1340. * Dieter Bayer
  1341. *
  1342. * DESCRIPTION
  1343. *
  1344. * Calculate the normal of a single element in the point P.
  1345. *
  1346. * CHANGES
  1347. *
  1348. * Jul 1994 : Creation (with help from Alexander Enzmann).
  1349. *
  1350. ******************************************************************************/
  1351. static void element_normal(VECTOR Result, VECTOR P, BLOB_ELEMENT *Element)
  1352. {
  1353. DBL val, dist;
  1354. VECTOR V1, PP;
  1355. switch (Element->Type)
  1356. {
  1357. case BLOB_SPHERE:
  1358. VSub(V1, P, Element->O);
  1359. VDot(dist, V1, V1);
  1360. if (dist <= Element->rad2)
  1361. {
  1362. val = -2.0 * Element->c[0] * dist - Element->c[1];
  1363. VAddScaledEq(Result, val, V1);
  1364. }
  1365. break;
  1366. case BLOB_ELLIPSOID:
  1367. MInvTransPoint(PP, P, Element->Trans);
  1368. VSub(V1, PP, Element->O);
  1369. VDot(dist, V1, V1);
  1370. if (dist <= Element->rad2)
  1371. {
  1372. val = -2.0 * Element->c[0] * dist - Element->c[1];
  1373. MTransNormal(V1, V1, Element->Trans);
  1374. VAddScaledEq(Result, val, V1);
  1375. }
  1376. break;
  1377. case BLOB_BASE_HEMISPHERE:
  1378. MInvTransPoint(PP, P, Element->Trans);
  1379. if (PP[Z] <= 0.0)
  1380. {
  1381. VDot(dist, PP, PP);
  1382. if (dist <= Element->rad2)
  1383. {
  1384. val = -2.0 * Element->c[0] * dist - Element->c[1];
  1385. MTransNormal(PP, PP, Element->Trans);
  1386. VAddScaledEq(Result, val, PP);
  1387. }
  1388. }
  1389. break;
  1390. case BLOB_APEX_HEMISPHERE:
  1391. MInvTransPoint(PP, P, Element->Trans);
  1392. PP[Z] -= Element->len;
  1393. if (PP[Z] >= 0.0)
  1394. {
  1395. VDot(dist, PP, PP);
  1396. if (dist <= Element->rad2)
  1397. {
  1398. val = -2.0 * Element->c[0] * dist - Element->c[1];
  1399. MTransNormal(PP, PP, Element->Trans);
  1400. VAddScaledEq(Result, val, PP);
  1401. }
  1402. }
  1403. break;
  1404. case BLOB_CYLINDER:
  1405. MInvTransPoint(PP, P, Element->Trans);
  1406. if ((PP[Z] >= 0.0) && (PP[Z] <= Element->len))
  1407. {
  1408. if ((dist = Sqr(PP[X]) + Sqr(PP[Y])) <= Element->rad2)
  1409. {
  1410. val = -2.0 * Element->c[0] * dist - Element->c[1];
  1411. PP[Z] = 0.0;
  1412. MTransNormal(PP, PP, Element->Trans);
  1413. VAddScaledEq(Result, val, PP);
  1414. }
  1415. }
  1416. break;
  1417. }
  1418. }
  1419. /*****************************************************************************
  1420. *
  1421. * FUNCTION
  1422. *
  1423. * Blob_Normal
  1424. *
  1425. * INPUT
  1426. *
  1427. * Object - Pointer to blob structure
  1428. * Inter - Pointer to intersection
  1429. *
  1430. * OUTPUT
  1431. *
  1432. * Result - Blob's normal
  1433. *
  1434. * RETURNS
  1435. *
  1436. * AUTHOR
  1437. *
  1438. * Alexander Enzmann
  1439. *
  1440. * DESCRIPTION
  1441. *
  1442. * Calculate the blob's surface normal in the intersection point.
  1443. *
  1444. * CHANGES
  1445. *
  1446. * Jul 1994 : Added code for bounding hierarchy traversal. [DB]
  1447. *
  1448. ******************************************************************************/
  1449. static void Blob_Normal(VECTOR Result, OBJECT *Object, INTERSECTION *Inter)
  1450. {
  1451. int i;
  1452. int size;
  1453. DBL dist, val;
  1454. VECTOR New_Point, V1;
  1455. BLOB *Blob = (BLOB *) Object;
  1456. BSPHERE_TREE *Tree;
  1457. /* Transform the point into the blob space. */
  1458. if (Blob->Trans != NULL)
  1459. {
  1460. MInvTransPoint(New_Point, Inter->IPoint, Blob->Trans);
  1461. }
  1462. else
  1463. {
  1464. Assign_Vector(New_Point, Inter->IPoint);
  1465. }
  1466. Make_Vector(Result, 0.0, 0.0, 0.0);
  1467. /* For each component that contributes to this point, add its bit to the normal */
  1468. if (Blob->Data->Tree == NULL)
  1469. {
  1470. /* There's no tree --> step through all elements. */
  1471. for (i = 0; i < Blob->Data->Number_Of_Components; i++)
  1472. {
  1473. element_normal(Result, New_Point, &(Blob->Data->Entry[i]));
  1474. }
  1475. }
  1476. else
  1477. {
  1478. /* A tree exists --> step through the tree. */
  1479. size = 0;
  1480. Queue[size++] = Blob->Data->Tree;
  1481. while (size > 0)
  1482. {
  1483. Tree = Queue[--size];
  1484. /* Test if current node is a leaf. */
  1485. if (Tree->Entries <= 0)
  1486. {
  1487. element_normal(Result, New_Point, (BLOB_ELEMENT *)Tree->Node);
  1488. }
  1489. else
  1490. {
  1491. /* Test all sub-nodes. */
  1492. for (i = 0; i < (int)Tree->Entries; i++)
  1493. {
  1494. /* Insert sub-node if we are inside. */
  1495. VSub(V1, New_Point, Tree->Node[i]->C);
  1496. VDot(dist, V1, V1);
  1497. if (dist <= Tree->Node[i]->r2)
  1498. {
  1499. insert_node(Tree->Node[i], &size);
  1500. }
  1501. }
  1502. }
  1503. }
  1504. }
  1505. VDot(val, Result, Result);
  1506. if (val == 0.0)
  1507. {
  1508. Make_Vector(Result, 1.0, 0.0, 0.0);
  1509. }
  1510. else
  1511. {
  1512. /* Normalize normal vector. */
  1513. val = 1.0 / sqrt(val);
  1514. VScaleEq(Result, val);
  1515. }
  1516. /* Transform back to world space. */
  1517. if (Blob->Trans != NULL)
  1518. {
  1519. MTransNormal(Result, Result, Blob->Trans);
  1520. VNormalize(Result, Result);
  1521. }
  1522. }
  1523. /*****************************************************************************
  1524. *
  1525. * FUNCTION
  1526. *
  1527. * Translate_Blob
  1528. *
  1529. * INPUT
  1530. *
  1531. * Vector - Translation vector
  1532. *
  1533. * OUTPUT
  1534. *
  1535. * Object - Pointer to blob structure
  1536. *
  1537. * RETURNS
  1538. *
  1539. * AUTHOR
  1540. *
  1541. * Alexander Enzmann
  1542. *
  1543. * DESCRIPTION
  1544. *
  1545. * Translate a blob.
  1546. *
  1547. * CHANGES
  1548. *
  1549. * -
  1550. *
  1551. ******************************************************************************/
  1552. static void Translate_Blob(OBJECT *Object, VECTOR Vector, TRANSFORM *Trans)
  1553. {
  1554. Transform_Blob(Object, Trans);
  1555. }
  1556. /*****************************************************************************
  1557. *
  1558. * FUNCTION
  1559. *
  1560. * Rotate_Blob
  1561. *
  1562. * INPUT
  1563. *
  1564. * Vector - Rotation vector
  1565. *
  1566. * OUTPUT
  1567. *
  1568. * Object - Pointer to blob structure
  1569. *
  1570. * RETURNS
  1571. *
  1572. * AUTHOR
  1573. *
  1574. * Alexander Enzmann
  1575. *
  1576. * DESCRIPTION
  1577. *
  1578. * Rotate a blob.
  1579. *
  1580. * CHANGES
  1581. *
  1582. * -
  1583. *
  1584. ******************************************************************************/
  1585. static void Rotate_Blob(OBJECT *Object, VECTOR Vector, TRANSFORM *Trans)
  1586. {
  1587. Transform_Blob(Object, Trans);
  1588. }
  1589. /*****************************************************************************
  1590. *
  1591. * FUNCTION
  1592. *
  1593. * Scale_Blob
  1594. *
  1595. * INPUT
  1596. *
  1597. * Vector - Scaling vector
  1598. *
  1599. * OUTPUT
  1600. *
  1601. * Object - Pointer to blob structure
  1602. *
  1603. * RETURNS
  1604. *
  1605. * AUTHOR
  1606. *
  1607. * Alexander Enzmann
  1608. *
  1609. * DESCRIPTION
  1610. *
  1611. * Scale a blob.
  1612. *
  1613. * CHANGES
  1614. *
  1615. * -
  1616. *
  1617. ******************************************************************************/
  1618. static void Scale_Blob(OBJECT *Object, VECTOR Vector, TRANSFORM *Trans)
  1619. {
  1620. Transform_Blob(Object, Trans);
  1621. }
  1622. /*****************************************************************************
  1623. *
  1624. * FUNCTION
  1625. *
  1626. * Transform_Blob
  1627. *
  1628. * INPUT
  1629. *
  1630. * Trans - Pointer to transformation
  1631. *
  1632. * OUTPUT
  1633. *
  1634. * Object - Pointer to blob structure
  1635. *
  1636. * RETURNS
  1637. *
  1638. * AUTHOR
  1639. *
  1640. * Alexander Enzmann
  1641. *
  1642. * DESCRIPTION
  1643. *
  1644. * Transform a blob.
  1645. *
  1646. * CHANGES
  1647. *
  1648. * -
  1649. *
  1650. ******************************************************************************/
  1651. static void Transform_Blob(OBJECT *Object, TRANSFORM *Trans)
  1652. {
  1653. int i;
  1654. BLOB *Blob = (BLOB *)Object;
  1655. if (Blob->Trans == NULL)
  1656. {
  1657. Blob->Trans = Create_Transform();
  1658. }
  1659. Recompute_BBox(&Object->BBox, Trans);
  1660. Compose_Transforms(Blob->Trans, Trans);
  1661. for (i = 0; i < Blob->Data->Number_Of_Components; i++)
  1662. {
  1663. Transform_Textures(Blob->Element_Texture[i], Trans);
  1664. }
  1665. }
  1666. /*****************************************************************************
  1667. *
  1668. * FUNCTION
  1669. *
  1670. * Invert_Blob
  1671. *
  1672. * INPUT
  1673. *
  1674. * Object - Pointer to blob structure
  1675. *
  1676. * OUTPUT
  1677. *
  1678. * Object
  1679. *
  1680. * RETURNS
  1681. *
  1682. * AUTHOR
  1683. *
  1684. * Alexander Enzmann
  1685. *
  1686. * DESCRIPTION
  1687. *
  1688. * Invert a blob.
  1689. *
  1690. * CHANGES
  1691. *
  1692. * -
  1693. *
  1694. ******************************************************************************/
  1695. static void Invert_Blob(OBJECT *Object)
  1696. {
  1697. Invert_Flag(Object, INVERTED_FLAG);
  1698. }
  1699. /*****************************************************************************
  1700. *
  1701. * FUNCTION
  1702. *
  1703. * Create_Blob
  1704. *
  1705. * INPUT
  1706. *
  1707. * Object - Pointer to blob structure
  1708. *
  1709. * OUTPUT
  1710. *
  1711. * Object
  1712. *
  1713. * RETURNS
  1714. *
  1715. * AUTHOR
  1716. *
  1717. * Alexander Enzmann
  1718. *
  1719. * DESCRIPTION
  1720. *
  1721. * Create a new blob.
  1722. *
  1723. * CHANGES
  1724. *
  1725. * -
  1726. *
  1727. ******************************************************************************/
  1728. BLOB *Create_Blob()
  1729. {
  1730. BLOB *New;
  1731. New = (BLOB *)POV_MALLOC(sizeof (BLOB), "blob");
  1732. INIT_OBJECT_FIELDS(New, BLOB_OBJECT, &Blob_Methods)
  1733. Set_Flag(New, HIERARCHY_FLAG);
  1734. New->Trans = NULL;
  1735. New->Data = NULL;
  1736. New->Element_Texture = NULL;
  1737. return (New);
  1738. }
  1739. /*****************************************************************************
  1740. *
  1741. * FUNCTION
  1742. *
  1743. * Copy_Blob
  1744. *
  1745. * INPUT
  1746. *
  1747. * Object - Pointer to blob structure
  1748. *
  1749. * OUTPUT
  1750. *
  1751. * Object
  1752. *
  1753. * RETURNS
  1754. *
  1755. * AUTHOR
  1756. *
  1757. * Alexander Enzmann
  1758. *
  1759. * DESCRIPTION
  1760. *
  1761. * Copy a blob.
  1762. *
  1763. * NOTE: The components are not copied, only the number of references is
  1764. * counted, so that Destroy_Blob() knows if they can be destroyed.
  1765. *
  1766. * CHANGES
  1767. *
  1768. * Jul 1994 : Added code for blob data reference counting. [DB]
  1769. *
  1770. ******************************************************************************/
  1771. static BLOB *Copy_Blob(OBJECT *Object)
  1772. {
  1773. int i;
  1774. BLOB *New, *Old = (BLOB *)Object;
  1775. New = Create_Blob();
  1776. /* Copy blob. */
  1777. *New = *Old;
  1778. New->Trans = Copy_Transform(New->Trans);
  1779. New->Data->References++;
  1780. New->Element_Texture = (TEXTURE **)POV_MALLOC(New->Data->Number_Of_Components*sizeof(TEXTURE *), "blob texture list");
  1781. for (i = 0; i < New->Data->Number_Of_Components; i++)
  1782. {
  1783. New->Element_Texture[i] = Copy_Textures(Old->Element_Texture[i]);
  1784. }
  1785. return (New);
  1786. }
  1787. /*****************************************************************************
  1788. *
  1789. * FUNCTION
  1790. *
  1791. * Create_Blob_List_Element
  1792. *
  1793. * INPUT
  1794. *
  1795. * OUTPUT
  1796. *
  1797. * RETURNS
  1798. *
  1799. * BLOB_LIST * - Pointer to blob element
  1800. *
  1801. * AUTHOR
  1802. *
  1803. * Dieter Bayer
  1804. *
  1805. * DESCRIPTION
  1806. *
  1807. * Create a new blob element in the component list used during parsing.
  1808. *
  1809. * CHANGES
  1810. *
  1811. * Sep 1994 : Creation.
  1812. *
  1813. ******************************************************************************/
  1814. BLOB_LIST *Create_Blob_List_Element()
  1815. {
  1816. BLOB_LIST *New;
  1817. New = (BLOB_LIST *)POV_MALLOC(sizeof(BLOB_LIST), "blob component");
  1818. init_blob_element(&New->elem);
  1819. return (New);
  1820. }
  1821. /*****************************************************************************
  1822. *
  1823. * FUNCTION
  1824. *
  1825. * Destroy_Blob
  1826. *
  1827. * INPUT
  1828. *
  1829. * Object - Pointer to blob structure
  1830. *
  1831. * OUTPUT
  1832. *
  1833. * Object
  1834. *
  1835. * RETURNS
  1836. *
  1837. * AUTHOR
  1838. *
  1839. * Alexander Enzmann
  1840. *
  1841. * DESCRIPTION
  1842. *
  1843. * Destroy a blob.
  1844. *
  1845. * NOTE: The blob data is destroyed if they are no longer used by any copy.
  1846. *
  1847. * CHANGES
  1848. *
  1849. * Jul 1994 : Added code for blob data reference counting. [DB]
  1850. *
  1851. * Dec 1994 : Fixed memory leakage. [DB]
  1852. *
  1853. * Aug 1995 : Fixed freeing of already freed memory. [DB]
  1854. *
  1855. ******************************************************************************/
  1856. static void Destroy_Blob(OBJECT *Object)
  1857. {
  1858. int i;
  1859. BLOB *Blob = (BLOB *)Object;
  1860. Destroy_Transform(Blob->Trans);
  1861. for (i = 0; i < Blob->Data->Number_Of_Components; i++)
  1862. {
  1863. Destroy_Textures(Blob->Element_Texture[i]);
  1864. }
  1865. POV_FREE(Blob->Element_Texture);
  1866. if (--(Blob->Data->References) == 0)
  1867. {
  1868. Destroy_Bounding_Sphere_Hierarchy(Blob->Data->Tree);
  1869. for (i = 0; i < Blob->Data->Number_Of_Components; i++)
  1870. {
  1871. /*
  1872. * Make sure to destroy multiple references of a texture
  1873. * and/or transformation only once. Multiple references
  1874. * are only used with cylindrical blobs. Thus it's
  1875. * enough to ignore all cylinder caps.
  1876. */
  1877. if ((Blob->Data->Entry[i].Type == BLOB_SPHERE) ||
  1878. (Blob->Data->Entry[i].Type == BLOB_ELLIPSOID) ||
  1879. (Blob->Data->Entry[i].Type == BLOB_CYLINDER))
  1880. {
  1881. Destroy_Transform(Blob->Data->Entry[i].Trans);
  1882. Blob->Data->Entry[i].Trans = NULL;
  1883. Blob->Data->Entry[i].Texture = NULL;
  1884. }
  1885. }
  1886. POV_FREE(Blob->Data->Entry);
  1887. POV_FREE(Blob->Data->Intervals);
  1888. POV_FREE(Blob->Data);
  1889. }
  1890. POV_FREE(Object);
  1891. }
  1892. /*****************************************************************************
  1893. *
  1894. * FUNCTION
  1895. *
  1896. * Compute_Blob_BBox
  1897. *
  1898. * INPUT
  1899. *
  1900. * Blob - Blob
  1901. *
  1902. * OUTPUT
  1903. *
  1904. * Blob
  1905. *
  1906. * RETURNS
  1907. *
  1908. * AUTHOR
  1909. *
  1910. * Dieter Bayer
  1911. *
  1912. * DESCRIPTION
  1913. *
  1914. * Calculate the bounding box of a blob.
  1915. *
  1916. * CHANGES
  1917. *
  1918. * Aug 1994 : Creation.
  1919. *
  1920. ******************************************************************************/
  1921. static void Compute_Blob_BBox(BLOB *Blob)
  1922. {
  1923. int i;
  1924. DBL radius, radius2;
  1925. VECTOR Center, Min, Max;
  1926. Make_Vector(Min, BOUND_HUGE, BOUND_HUGE, BOUND_HUGE);
  1927. Make_Vector(Max, - BOUND_HUGE, - BOUND_HUGE, - BOUND_HUGE);
  1928. for (i = 0; i < Blob->Data->Number_Of_Components; i++)
  1929. {
  1930. if (Blob->Data->Entry[i].c[2] > 0.0)
  1931. {
  1932. get_element_bounding_sphere(&Blob->Data->Entry[i], Center, &radius2);
  1933. radius = sqrt(radius2);
  1934. Min[X] = min(Min[X], Center[X] - radius);
  1935. Min[Y] = min(Min[Y], Center[Y] - radius);
  1936. Min[Z] = min(Min[Z], Center[Z] - radius);
  1937. Max[X] = max(Max[X], Center[X] + radius);
  1938. Max[Y] = max(Max[Y], Center[Y] + radius);
  1939. Max[Z] = max(Max[Z], Center[Z] + radius);
  1940. }
  1941. }
  1942. Make_BBox_from_min_max(Blob->BBox, Min, Max);
  1943. if (Blob->Trans != NULL)
  1944. {
  1945. Recompute_BBox(&Blob->BBox, Blob->Trans);
  1946. }
  1947. }
  1948. /*****************************************************************************
  1949. *
  1950. * FUNCTION
  1951. *
  1952. * get_element_bounding_sphere
  1953. *
  1954. * INPUT
  1955. *
  1956. * Element - Pointer to element
  1957. * Center - Bounding sphere's center
  1958. * Radius2 - Bounding sphere's squared radius
  1959. *
  1960. * OUTPUT
  1961. *
  1962. * Center, Radius2
  1963. *
  1964. * RETURNS
  1965. *
  1966. * AUTHOR
  1967. *
  1968. * Dieter Bayer
  1969. *
  1970. * DESCRIPTION
  1971. *
  1972. * Calculate the bounding sphere of a blob element.
  1973. *
  1974. * CHANGES
  1975. *
  1976. * Sep 1994 : Creation.
  1977. *
  1978. ******************************************************************************/
  1979. static void get_element_bounding_sphere(BLOB_ELEMENT *Element, VECTOR Center, DBL *Radius2)
  1980. {
  1981. DBL r, r2 = 0.0;
  1982. VECTOR C, H;
  1983. switch (Element->Type)
  1984. {
  1985. case BLOB_SPHERE:
  1986. case BLOB_ELLIPSOID:
  1987. r2 = Element->rad2;
  1988. Assign_Vector(C, Element->O);
  1989. break;
  1990. case BLOB_BASE_HEMISPHERE:
  1991. r2 = Element->rad2;
  1992. Make_Vector(C, 0.0, 0.0, 0.0);
  1993. break;
  1994. case BLOB_APEX_HEMISPHERE:
  1995. r2 = Element->rad2;
  1996. Make_Vector(C, 0.0, 0.0, Element->len);
  1997. break;
  1998. case BLOB_CYLINDER :
  1999. Make_Vector(C, 0.0, 0.0, 0.5 * Element->len);
  2000. r2 = Element->rad2 + Sqr(0.5 * Element->len);
  2001. break;
  2002. }
  2003. /* Transform bounding sphere if necessary. */
  2004. if (Element->Trans != NULL)
  2005. {
  2006. r = sqrt(r2);
  2007. MTransPoint(C, C, Element->Trans);
  2008. Make_Vector(H, r, r, r);
  2009. MTransDirection(H, H, Element->Trans);
  2010. r = max(max(fabs(H[X]), fabs(H[Y])), fabs(H[Z]));
  2011. r2 = Sqr(r) + EPSILON;
  2012. }
  2013. Assign_Vector(Center, C);
  2014. *Radius2 = r2;
  2015. }
  2016. /*****************************************************************************
  2017. *
  2018. * FUNCTION
  2019. *
  2020. * init_blob_element
  2021. *
  2022. * INPUT
  2023. *
  2024. * Element - Pointer to blob element
  2025. *
  2026. * OUTPUT
  2027. *
  2028. * Element
  2029. *
  2030. * RETURNS
  2031. *
  2032. * AUTHOR
  2033. *
  2034. * Dieter Bayer
  2035. *
  2036. * DESCRIPTION
  2037. *
  2038. * Init blob element.
  2039. *
  2040. * CHANGES
  2041. *
  2042. * Sep 1994 : Creation.
  2043. *
  2044. ******************************************************************************/
  2045. static void init_blob_element(BLOB_ELEMENT *Element)
  2046. {
  2047. Element->Type = 0;
  2048. Element->index = 0;
  2049. Element->len =
  2050. Element->rad2 = 0.0;
  2051. Element->c[0] =
  2052. Element->c[1] =
  2053. Element->c[2] =
  2054. Element->f[0] =
  2055. Element->f[1] =
  2056. Element->f[2] =
  2057. Element->f[3] =
  2058. Element->f[4] = 0.0;
  2059. Make_Vector(Element->O, 0.0, 0.0, 0.0);
  2060. Element->Texture = NULL;
  2061. Element->Trans = NULL;
  2062. }
  2063. /*****************************************************************************
  2064. *
  2065. * FUNCTION
  2066. *
  2067. * Make_Blob
  2068. *
  2069. * INPUT
  2070. *
  2071. * Blob - Pointer to blob structure
  2072. * threshold - Blob's threshold
  2073. * BlobList - Pointer to elements
  2074. * npoints - Number of elements
  2075. *
  2076. * OUTPUT
  2077. *
  2078. * Blob
  2079. *
  2080. * RETURNS
  2081. *
  2082. * AUTHOR
  2083. *
  2084. * Alexander Enzmann
  2085. *
  2086. * DESCRIPTION
  2087. *
  2088. * Create a blob after it was read from the scene file.
  2089. *
  2090. * Starting with the density function: (1-r^2)^2, we have a field
  2091. * that varies in strength from 1 at r = 0 to 0 at r = 1. By
  2092. * substituting r/rad for r, we can adjust the range of influence
  2093. * of a particular component. By multiplication by coeff, we can
  2094. * adjust the amount of total contribution, giving the formula:
  2095. *
  2096. * coeff * (1 - (r/rad)^2)^2
  2097. *
  2098. * This varies in strength from coeff at r = 0, to 0 at r = rad.
  2099. *
  2100. * CHANGES
  2101. *
  2102. * Jul 1994 : Added code for cylindrical and ellipsoidical blobs. [DB]
  2103. *
  2104. ******************************************************************************/
  2105. void Make_Blob(BLOB *Blob, DBL threshold, BLOB_LIST *BlobList, int npoints)
  2106. {
  2107. int i, count;
  2108. DBL rad2, coeff;
  2109. BLOB_LIST *temp;
  2110. BLOB_ELEMENT *Entry;
  2111. if (npoints < 1)
  2112. {
  2113. Error("Need at least one component in a blob.");
  2114. }
  2115. /* Figure out how many components there will be. */
  2116. temp = BlobList;
  2117. for (i = count = 0; i < npoints; i++)
  2118. {
  2119. if (temp->elem.Type & BLOB_CYLINDER)
  2120. {
  2121. count += 3;
  2122. }
  2123. else
  2124. {
  2125. count++;
  2126. }
  2127. temp = temp->next;
  2128. /* Test for too many components. [DB 12/94] */
  2129. if (count >= MAX_BLOB_COMPONENTS)
  2130. {
  2131. Error("There are more than %d components in a blob.\n", MAX_BLOB_COMPONENTS);
  2132. }
  2133. }
  2134. /* Initialize the blob data. */
  2135. Blob->Data->Threshold = threshold;
  2136. Entry = Blob->Data->Entry;
  2137. for (i = 0; i < npoints; i++)
  2138. {
  2139. temp = BlobList;
  2140. if ((fabs(temp->elem.c[2]) < EPSILON) || (temp->elem.rad2 < EPSILON))
  2141. {
  2142. Warning(0.0, "Degenerate Blob element\n");
  2143. }
  2144. /* Initialize component. */
  2145. *Entry = temp->elem;
  2146. /* We have a multi-texture blob. */
  2147. if (Entry->Texture != NULL)
  2148. {
  2149. Set_Flag(Blob, MULTITEXTURE_FLAG);
  2150. }
  2151. /* Store blob specific information. */
  2152. switch (temp->elem.Type)
  2153. {
  2154. case BLOB_ELLIPSOID :
  2155. case BLOB_SPHERE :
  2156. rad2 = temp->elem.rad2;
  2157. coeff = temp->elem.c[2];
  2158. Entry->c[0] = coeff / (rad2 * rad2);
  2159. Entry->c[1] = -(2.0 * coeff) / rad2;
  2160. Entry->c[2] = coeff;
  2161. Entry++;
  2162. break;
  2163. case BLOB_CYLINDER :
  2164. rad2 = temp->elem.rad2;
  2165. coeff = temp->elem.c[2];
  2166. /* Create cylindrical component. */
  2167. Entry->c[0] = coeff / (rad2 * rad2);
  2168. Entry->c[1] = -(2.0 * coeff) / rad2;
  2169. Entry->c[2] = coeff;
  2170. Entry++;
  2171. /* Create hemispherical component at the base. */
  2172. *Entry = temp->elem;
  2173. Entry->Type = BLOB_BASE_HEMISPHERE;
  2174. Entry->c[0] = coeff / (rad2 * rad2);
  2175. Entry->c[1] = -(2.0 * coeff) / rad2;
  2176. Entry->c[2] = coeff;
  2177. Entry++;
  2178. /* Create hemispherical component at the apex. */
  2179. *Entry = temp->elem;
  2180. Entry->Type = BLOB_APEX_HEMISPHERE;
  2181. Entry->c[0] = coeff / (rad2 * rad2);
  2182. Entry->c[1] = -(2.0 * coeff) / rad2;
  2183. Entry->c[2] = coeff;
  2184. Entry++;
  2185. break;
  2186. default :
  2187. Error("Unknown blob component.\n");
  2188. }
  2189. /* Get rid of texture non longer needed. */
  2190. BlobList = BlobList->next;
  2191. Destroy_Textures(temp->elem.Texture);
  2192. POV_FREE(temp);
  2193. }
  2194. for (i = 0; i < count; i++)
  2195. {
  2196. Blob->Data->Entry[i].index = i;
  2197. }
  2198. /* Compute bounding box. */
  2199. Compute_Blob_BBox(Blob);
  2200. /* Create bounding sphere hierarchy. */
  2201. if (Test_Flag(Blob, HIERARCHY_FLAG))
  2202. {
  2203. build_bounding_hierarchy(Blob);
  2204. }
  2205. }
  2206. /*****************************************************************************
  2207. *
  2208. * FUNCTION
  2209. *
  2210. * Test_Blob_Opacity
  2211. *
  2212. * INPUT
  2213. *
  2214. * Blob - Pointer to blob structure
  2215. *
  2216. * OUTPUT
  2217. *
  2218. * Blob
  2219. *
  2220. * RETURNS
  2221. *
  2222. * AUTHOR
  2223. *
  2224. * Dieter Bayer
  2225. *
  2226. * DESCRIPTION
  2227. *
  2228. * Set the opacity flag of the blob according to the opacity
  2229. * of the blob's texture(s).
  2230. *
  2231. * CHANGES
  2232. *
  2233. * Apr 1996 : Creation.
  2234. *
  2235. ******************************************************************************/
  2236. void Test_Blob_Opacity(BLOB *Blob)
  2237. {
  2238. int i;
  2239. /* Initialize opacity flag to the opacity of the object's texture. */
  2240. if ((Blob->Texture == NULL) || (Test_Opacity(Blob->Texture)))
  2241. {
  2242. Set_Flag(Blob, OPAQUE_FLAG);
  2243. }
  2244. if (Test_Flag(Blob, MULTITEXTURE_FLAG))
  2245. {
  2246. for (i = 0; i < Blob->Data->Number_Of_Components; i++)
  2247. {
  2248. if (Blob->Element_Texture[i] != NULL)
  2249. {
  2250. /* If component's texture isn't opaque the blob is neither. */
  2251. if (!Test_Opacity(Blob->Element_Texture[i]))
  2252. {
  2253. Clear_Flag(Blob, OPAQUE_FLAG);
  2254. }
  2255. }
  2256. }
  2257. }
  2258. }
  2259. /*****************************************************************************
  2260. *
  2261. * FUNCTION
  2262. *
  2263. * build_bounding_hierarchy
  2264. *
  2265. * INPUT
  2266. *
  2267. * OUTPUT
  2268. *
  2269. * RETURNS
  2270. *
  2271. * AUTHOR
  2272. *
  2273. * Dieter Bayer
  2274. *
  2275. * DESCRIPTION
  2276. *
  2277. * Create the bounding sphere hierarchy.
  2278. *
  2279. * CHANGES
  2280. *
  2281. * Oct 1994 : Creation. (Derived from the bounding slab creation code)
  2282. *
  2283. ******************************************************************************/
  2284. static void build_bounding_hierarchy(BLOB *Blob)
  2285. {
  2286. int i, nElem, maxelements;
  2287. BSPHERE_TREE **Elements;
  2288. nElem = (int)Blob->Data->Number_Of_Components;
  2289. maxelements = 2 * nElem;
  2290. /*
  2291. * Now allocate an array to hold references to these elements.
  2292. */
  2293. Elements = (BSPHERE_TREE **)POV_MALLOC(maxelements*sizeof(BSPHERE_TREE *), "blob bounding hierarchy");
  2294. /* Init list with blob elements. */
  2295. for (i = 0; i < nElem; i++)
  2296. {
  2297. Elements[i] = (BSPHERE_TREE *)POV_MALLOC(sizeof(BSPHERE_TREE), "blob bounding hierarchy");
  2298. Elements[i]->Entries = 0;
  2299. Elements[i]->Node = (BSPHERE_TREE **)&Blob->Data->Entry[i];
  2300. get_element_bounding_sphere(&Blob->Data->Entry[i], Elements[i]->C, &Elements[i]->r2);
  2301. }
  2302. Build_Bounding_Sphere_Hierarchy(&Blob->Data->Tree, nElem, Elements);
  2303. /* Get rid of the Elements array. */
  2304. POV_FREE(Elements);
  2305. }
  2306. /*****************************************************************************
  2307. *
  2308. * FUNCTION
  2309. *
  2310. * Determine_Blob_Textures
  2311. *
  2312. * INPUT
  2313. *
  2314. * OUTPUT
  2315. *
  2316. * RETURNS
  2317. *
  2318. * AUTHOR
  2319. *
  2320. * Dieter Bayer
  2321. *
  2322. * DESCRIPTION
  2323. *
  2324. * Determine the textures and weights of all components affecting
  2325. * the given intersection point. The weights are calculated from
  2326. * the field values and sum to 1.
  2327. *
  2328. * CHANGES
  2329. *
  2330. * Sep 1994 : Creation.
  2331. *
  2332. * Mar 1996 : Make the call to resize the textures/weights list just once
  2333. * at the beginning instead of doing it for every element. [DB]
  2334. *
  2335. ******************************************************************************/
  2336. void Determine_Blob_Textures(BLOB *Blob, VECTOR IPoint, int *Count, TEXTURE **Textures, DBL *Weights)
  2337. {
  2338. int i;
  2339. int size;
  2340. DBL rad2, sum;
  2341. VECTOR V1, P;
  2342. BLOB_ELEMENT *Element;
  2343. BSPHERE_TREE *Tree;
  2344. /* Make sure we have enough room in the textures/weights list. */
  2345. Reinitialize_Lighting_Code(Blob->Data->Number_Of_Components, &Textures, &Weights);
  2346. /* Transform the point into the blob space. */
  2347. if (Blob->Trans != NULL)
  2348. {
  2349. MInvTransPoint(P, IPoint, Blob->Trans);
  2350. }
  2351. else
  2352. {
  2353. Assign_Vector(P, IPoint);
  2354. }
  2355. *Count = 0;
  2356. if (Blob->Data->Tree == NULL)
  2357. {
  2358. /* There's no tree --> step through all elements. */
  2359. for (i = 0; i < Blob->Data->Number_Of_Components; i++)
  2360. {
  2361. Element = &Blob->Data->Entry[i];
  2362. determine_element_texture(Blob, Element, Blob->Element_Texture[i], P, Count, Textures, Weights);
  2363. }
  2364. }
  2365. else
  2366. {
  2367. /* A tree exists --> step through the tree. */
  2368. size = 0;
  2369. Queue[size++] = Blob->Data->Tree;
  2370. while (size > 0)
  2371. {
  2372. Tree = Queue[--size];
  2373. /* Test if current node is a leaf. */
  2374. if (Tree->Entries <= 0)
  2375. {
  2376. determine_element_texture(Blob, (BLOB_ELEMENT *)Tree->Node, Blob->Element_Texture[((BLOB_ELEMENT *)Tree->Node)->index], P, Count, Textures, Weights);
  2377. }
  2378. else
  2379. {
  2380. /* Test all sub-nodes. */
  2381. for (i = 0; i < (int)Tree->Entries; i++)
  2382. {
  2383. /* Insert sub-node if we are inside. */
  2384. VSub(V1, P, Tree->Node[i]->C);
  2385. VDot(rad2, V1, V1);
  2386. if (rad2 <= Tree->Node[i]->r2)
  2387. {
  2388. insert_node(Tree->Node[i], &size);
  2389. }
  2390. }
  2391. }
  2392. }
  2393. }
  2394. /* Normalize weights so that their sum is 1. */
  2395. if (*Count > 0)
  2396. {
  2397. sum = 0.0;
  2398. for (i = 0; i < *Count; i++)
  2399. {
  2400. sum += Weights[i];
  2401. }
  2402. if (sum > 0.0)
  2403. {
  2404. for (i = 0; i < *Count; i++)
  2405. {
  2406. Weights[i] /= sum;
  2407. }
  2408. }
  2409. }
  2410. }
  2411. /*****************************************************************************
  2412. *
  2413. * FUNCTION
  2414. *
  2415. * determine_element_texture
  2416. *
  2417. * INPUT
  2418. *
  2419. * OUTPUT
  2420. *
  2421. * RETURNS
  2422. *
  2423. * AUTHOR
  2424. *
  2425. * Dieter Bayer
  2426. *
  2427. * DESCRIPTION
  2428. *
  2429. * If the intersection point is inside the component calculate
  2430. * the field density and store the element's texture and the field
  2431. * value in the texture/weight list.
  2432. *
  2433. * CHANGES
  2434. *
  2435. * Sep 1994 : Creation.
  2436. *
  2437. ******************************************************************************/
  2438. static void determine_element_texture(BLOB *Blob, BLOB_ELEMENT *Element, TEXTURE *Texture, VECTOR P, int *Count, TEXTURE **Textures, DBL *Weights)
  2439. {
  2440. int i;
  2441. DBL density;
  2442. density = fabs(calculate_element_field(Element, P));
  2443. if (density > 0.0)
  2444. {
  2445. if (Texture == NULL)
  2446. {
  2447. Textures[*Count] = Blob->Texture;
  2448. }
  2449. else
  2450. {
  2451. Textures[*Count] = Texture;
  2452. }
  2453. /* Test if this texture is already used. */
  2454. for (i = 0; i < *Count; i++)
  2455. {
  2456. if (Textures[i] == Textures[*Count])
  2457. {
  2458. /* Add current weight to already existing texture weight. */
  2459. Weights[i] += density;
  2460. /* Any texture can only be in the list once --> exit. */
  2461. return;
  2462. }
  2463. }
  2464. Weights[(*Count)++] = density;
  2465. }
  2466. }
  2467. /*****************************************************************************
  2468. *
  2469. * FUNCTION
  2470. *
  2471. * Translate_Blob_Element
  2472. *
  2473. * INPUT
  2474. *
  2475. * Element - Pointer to blob element
  2476. * Vector - Translation vector
  2477. *
  2478. * OUTPUT
  2479. *
  2480. * Object
  2481. *
  2482. * RETURNS
  2483. *
  2484. * AUTHOR
  2485. *
  2486. * Dieter Bayer
  2487. *
  2488. * DESCRIPTION
  2489. *
  2490. * Translate a blob element.
  2491. *
  2492. * CHANGES
  2493. *
  2494. * Sep 1994 : Creation.
  2495. *
  2496. ******************************************************************************/
  2497. void Translate_Blob_Element(BLOB_ELEMENT *Element, VECTOR Vector)
  2498. {
  2499. TRANSFORM Trans;
  2500. Compute_Translation_Transform(&Trans, Vector);
  2501. if (Element->Trans == NULL)
  2502. {
  2503. /* This is a sphere component. */
  2504. VAddEq(Element->O, Vector);
  2505. }
  2506. else
  2507. {
  2508. /* This is one of the other components. */
  2509. Transform_Blob_Element(Element, &Trans);
  2510. }
  2511. Transform_Textures(Element->Texture, &Trans);
  2512. }
  2513. /*****************************************************************************
  2514. *
  2515. * FUNCTION
  2516. *
  2517. * Rotate_Blob_Element
  2518. *
  2519. * INPUT
  2520. *
  2521. * Element - Pointer to blob element
  2522. * Vector - Translation vector
  2523. *
  2524. * OUTPUT
  2525. *
  2526. * Object
  2527. *
  2528. * RETURNS
  2529. *
  2530. * AUTHOR
  2531. *
  2532. * Dieter Bayer
  2533. *
  2534. * DESCRIPTION
  2535. *
  2536. * Rotate a blob element.
  2537. *
  2538. * CHANGES
  2539. *
  2540. * Sep 1994 : Creation.
  2541. *
  2542. ******************************************************************************/
  2543. void Rotate_Blob_Element(BLOB_ELEMENT *Element, VECTOR Vector)
  2544. {
  2545. TRANSFORM Trans;
  2546. Compute_Rotation_Transform(&Trans, Vector);
  2547. if (Element->Trans == NULL)
  2548. {
  2549. /* This is a sphere component. */
  2550. MTransPoint(Element->O, Element->O, &Trans);
  2551. }
  2552. else
  2553. {
  2554. /* This is one of the other components. */
  2555. Transform_Blob_Element(Element, &Trans);
  2556. }
  2557. Transform_Textures(Element->Texture, &Trans);
  2558. }
  2559. /*****************************************************************************
  2560. *
  2561. * FUNCTION
  2562. *
  2563. * Scale_Blob_Element
  2564. *
  2565. * INPUT
  2566. *
  2567. * Element - Pointer to blob element
  2568. * Vector - Translation vector
  2569. *
  2570. * OUTPUT
  2571. *
  2572. * Object
  2573. *
  2574. * RETURNS
  2575. *
  2576. * AUTHOR
  2577. *
  2578. * Dieter Bayer
  2579. *
  2580. * DESCRIPTION
  2581. *
  2582. * Scale a blob element.
  2583. *
  2584. * CHANGES
  2585. *
  2586. * Sep 1994 : Creation.
  2587. *
  2588. ******************************************************************************/
  2589. void Scale_Blob_Element(BLOB_ELEMENT *Element, VECTOR Vector)
  2590. {
  2591. TRANSFORM Trans;
  2592. if ((Vector[X] != Vector[Y]) || (Vector[X] != Vector[Z]))
  2593. {
  2594. if (Element->Trans == NULL)
  2595. {
  2596. /* This is a sphere component --> change to ellipsoid component. */
  2597. Element->Type = BLOB_ELLIPSOID;
  2598. Element->Trans = Create_Transform();
  2599. }
  2600. }
  2601. Compute_Scaling_Transform(&Trans, Vector);
  2602. if (Element->Trans == NULL)
  2603. {
  2604. /* This is a sphere component. */
  2605. VScaleEq(Element->O, Vector[X]);
  2606. Element->rad2 *= Sqr(Vector[X]);
  2607. }
  2608. else
  2609. {
  2610. /* This is one of the other components. */
  2611. Transform_Blob_Element(Element, &Trans);
  2612. }
  2613. Transform_Textures(Element->Texture, &Trans);
  2614. }
  2615. /*****************************************************************************
  2616. *
  2617. * FUNCTION
  2618. *
  2619. * Transform_Blob_Element
  2620. *
  2621. * INPUT
  2622. *
  2623. * Element - Pointer to blob element
  2624. * Trans - Transformation
  2625. *
  2626. * OUTPUT
  2627. *
  2628. * Object
  2629. *
  2630. * RETURNS
  2631. *
  2632. * AUTHOR
  2633. *
  2634. * Dieter Bayer
  2635. *
  2636. * DESCRIPTION
  2637. *
  2638. * Transform a blob element.
  2639. *
  2640. * CHANGES
  2641. *
  2642. * Sep 1994 : Creation.
  2643. *
  2644. ******************************************************************************/
  2645. void Transform_Blob_Element(BLOB_ELEMENT *Element, TRANSFORM *Trans)
  2646. {
  2647. if (Element->Trans == NULL)
  2648. {
  2649. /* This is a sphere component --> change to ellipsoid component. */
  2650. Element->Type = BLOB_ELLIPSOID;
  2651. Element->Trans = Create_Transform();
  2652. }
  2653. Compose_Transforms(Element->Trans, Trans);
  2654. Transform_Textures(Element->Texture, Trans);
  2655. }
  2656. /*****************************************************************************
  2657. *
  2658. * FUNCTION
  2659. *
  2660. * Invert_Blob_Element
  2661. *
  2662. * INPUT
  2663. *
  2664. * Element - Pointer to blob element
  2665. *
  2666. * OUTPUT
  2667. *
  2668. * Object
  2669. *
  2670. * RETURNS
  2671. *
  2672. * AUTHOR
  2673. *
  2674. * Dieter Bayer
  2675. *
  2676. * DESCRIPTION
  2677. *
  2678. * Invert blob element by negating its strength.
  2679. *
  2680. * CHANGES
  2681. *
  2682. * Sep 1994 : Creation.
  2683. *
  2684. ******************************************************************************/
  2685. void Invert_Blob_Element(BLOB_ELEMENT *Element)
  2686. {
  2687. Element->c[2] *= -1.0;
  2688. }
  2689. /*****************************************************************************
  2690. *
  2691. * FUNCTION
  2692. *
  2693. * Init_Blob_Queue
  2694. *
  2695. * INPUT
  2696. *
  2697. * OUTPUT
  2698. *
  2699. * RETURNS
  2700. *
  2701. * AUTHOR
  2702. *
  2703. * Dieter Bayer
  2704. *
  2705. * DESCRIPTION
  2706. *
  2707. * Init queues for blob intersections.
  2708. *
  2709. * CHANGES
  2710. *
  2711. * Dec 1994 : Creation.
  2712. *
  2713. ******************************************************************************/
  2714. void Init_Blob_Queue()
  2715. {
  2716. Queue = (BSPHERE_TREE **)POV_MALLOC(Max_Queue_Size*sizeof(BSPHERE_TREE *), "blob queue");
  2717. }
  2718. /*****************************************************************************
  2719. *
  2720. * FUNCTION
  2721. *
  2722. * Destroy_Blob_Queue
  2723. *
  2724. * INPUT
  2725. *
  2726. * OUTPUT
  2727. *
  2728. * RETURNS
  2729. *
  2730. * AUTHOR
  2731. *
  2732. * Dieter Bayer
  2733. *
  2734. * DESCRIPTION
  2735. *
  2736. * Destroy queues for blob intersections.
  2737. *
  2738. * CHANGES
  2739. *
  2740. * Dec 1994 : Creation.
  2741. *
  2742. ******************************************************************************/
  2743. void Destroy_Blob_Queue()
  2744. {
  2745. if (Queue != NULL)
  2746. {
  2747. POV_FREE(Queue);
  2748. }
  2749. Queue = NULL;
  2750. }
  2751. /*****************************************************************************
  2752. *
  2753. * FUNCTION
  2754. *
  2755. * insert_node
  2756. *
  2757. * INPUT
  2758. *
  2759. * OUTPUT
  2760. *
  2761. * RETURNS
  2762. *
  2763. * AUTHOR
  2764. *
  2765. * Dieter Bayer
  2766. *
  2767. * DESCRIPTION
  2768. *
  2769. * Insert a node into the node queue.
  2770. *
  2771. * CHANGES
  2772. *
  2773. * Feb 1995 : Creation.
  2774. *
  2775. ******************************************************************************/
  2776. static void insert_node(BSPHERE_TREE *Node, int *size)
  2777. {
  2778. /* Resize queue if necessary. */
  2779. if (*size >= (int)Max_Queue_Size)
  2780. {
  2781. if (Max_Queue_Size >= INT_MAX/2)
  2782. {
  2783. Error("Blob queue overflow!\n");
  2784. }
  2785. Max_Queue_Size *= 2;
  2786. Queue = (BSPHERE_TREE **)POV_REALLOC(Queue, Max_Queue_Size*sizeof(BSPHERE_TREE *), "blob queue");
  2787. }
  2788. Queue[(*size)++] = Node;
  2789. }
  2790. /*****************************************************************************
  2791. *
  2792. * FUNCTION
  2793. *
  2794. * Create_Blob_Element_Texture_List
  2795. *
  2796. * INPUT
  2797. *
  2798. * OUTPUT
  2799. *
  2800. * RETURNS
  2801. *
  2802. * AUTHOR
  2803. *
  2804. * Dieter Bayer
  2805. *
  2806. * DESCRIPTION
  2807. *
  2808. * Create a list of all textures in the blob.
  2809. *
  2810. * The list actually contains copies of the textures not
  2811. * just references to them.
  2812. *
  2813. * CHANGES
  2814. *
  2815. * Mar 1996 : Created.
  2816. *
  2817. ******************************************************************************/
  2818. void Create_Blob_Element_Texture_List(BLOB *Blob, BLOB_LIST *BlobList, int npoints)
  2819. {
  2820. int i, element_count, count;
  2821. BLOB_LIST *temp;
  2822. if (npoints < 1)
  2823. {
  2824. Error("Need at least one component in a blob.");
  2825. }
  2826. /* Figure out how many components there will be. */
  2827. temp = BlobList;
  2828. for (i = count = 0; i < npoints; i++)
  2829. {
  2830. if (temp->elem.Type & BLOB_CYLINDER)
  2831. {
  2832. count += 3;
  2833. }
  2834. else
  2835. {
  2836. count++;
  2837. }
  2838. temp = temp->next;
  2839. /* Test for too many components. [DB 12/94] */
  2840. if (count >= MAX_BLOB_COMPONENTS)
  2841. {
  2842. Error("There are more than %d components in a blob.\n", MAX_BLOB_COMPONENTS);
  2843. }
  2844. }
  2845. /* Allocate memory for components. */
  2846. Blob->Data = (BLOB_DATA *)POV_MALLOC(sizeof(BLOB_DATA), "blob data");
  2847. Blob->Data->References = 1;
  2848. Blob->Data->Number_Of_Components = count;
  2849. Blob->Data->Entry = (BLOB_ELEMENT *)POV_MALLOC(count*sizeof(BLOB_ELEMENT), "blob data");
  2850. Blob->Data->Intervals = (BLOB_INTERVAL *)POV_MALLOC(2*Blob->Data->Number_Of_Components*sizeof(BLOB_INTERVAL), "blob intervals");
  2851. Blob->Data->Tree = NULL;
  2852. /* Init components. */
  2853. for (i = 0; i < count; i++)
  2854. {
  2855. init_blob_element(&Blob->Data->Entry[i]);
  2856. }
  2857. /* Allocate memory for list. */
  2858. Blob->Element_Texture = (TEXTURE **)POV_MALLOC(count*sizeof(TEXTURE *), "blob texture list");
  2859. for (i = 0; i < count; i++)
  2860. {
  2861. Blob->Element_Texture[i] = NULL;
  2862. }
  2863. for (i = element_count = 0; i < npoints; i++)
  2864. {
  2865. temp = BlobList;
  2866. /* Copy textures. */
  2867. switch (temp->elem.Type)
  2868. {
  2869. case BLOB_ELLIPSOID :
  2870. case BLOB_SPHERE :
  2871. /*
  2872. * Copy texture into element texture list. This is neccessary
  2873. * because individual textures have to be transformed too if
  2874. * copies of the blob are transformed.
  2875. */
  2876. Blob->Element_Texture[element_count++] = Copy_Textures(temp->elem.Texture);
  2877. break;
  2878. case BLOB_CYLINDER :
  2879. /*
  2880. * Copy texture into element texture list. This is neccessary
  2881. * because individual textures have to be transformed too if
  2882. * copies of the blob are transformed.
  2883. */
  2884. Blob->Element_Texture[element_count++] = Copy_Textures(temp->elem.Texture);
  2885. Blob->Element_Texture[element_count++] = Copy_Textures(temp->elem.Texture);
  2886. Blob->Element_Texture[element_count++] = Copy_Textures(temp->elem.Texture);
  2887. break;
  2888. }
  2889. BlobList = BlobList->next;
  2890. }
  2891. }