/src/org/mt4j/components/visibleComponents/shapes/MTPolygon.java

http://mt4j.googlecode.com/ · Java · 693 lines · 360 code · 101 blank · 232 comment · 73 complexity · ad48ac71dd64784a8383a1f476672cab MD5 · raw file

  1. /***********************************************************************
  2. * mt4j Copyright (c) 2008 - 2009, C.Ruff, Fraunhofer-Gesellschaft All rights reserved.
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. *
  17. ***********************************************************************/
  18. package org.mt4j.components.visibleComponents.shapes;
  19. import java.nio.FloatBuffer;
  20. import java.nio.IntBuffer;
  21. import javax.media.opengl.GL;
  22. import org.mt4j.components.bounds.BoundingSphere;
  23. import org.mt4j.components.bounds.IBoundingShape;
  24. import org.mt4j.components.bounds.OrientedBoundingBox;
  25. import org.mt4j.components.css.style.CSSStyle;
  26. import org.mt4j.util.MT4jSettings;
  27. import org.mt4j.util.MTColor;
  28. import org.mt4j.util.math.BezierVertex;
  29. import org.mt4j.util.math.Ray;
  30. import org.mt4j.util.math.Tools3D;
  31. import org.mt4j.util.math.ToolsGeometry;
  32. import org.mt4j.util.math.Vector3D;
  33. import org.mt4j.util.math.Vertex;
  34. import org.mt4j.util.opengl.GLTexture;
  35. import processing.core.PApplet;
  36. import processing.core.PGraphics;
  37. /**
  38. * This class represents a planar, convex polygon. The user of this class
  39. * is responsible for the polygon being planar and convex.
  40. * Methods like picking and others depend on these facts.
  41. * If <code>setNoFill(true)</code> is used and the polygon isnt closed, the
  42. * class can also be used to display a poly-line.
  43. *
  44. * @author Christopher Ruff
  45. */
  46. public class MTPolygon extends MTCSSStylableShape{
  47. /** The normal. */
  48. private Vector3D normal;
  49. /** The normal dirty. */
  50. private boolean normalDirty;
  51. /**
  52. * Instantiates a new mT polygon.
  53. *
  54. * @param vertices the vertices
  55. * @param pApplet the applet
  56. * @deprecated constructor will be deleted! Please , use the constructor with the PApplet instance as the first parameter.
  57. */
  58. public MTPolygon(Vertex[] vertices, PApplet pApplet) {
  59. this(pApplet,vertices);
  60. }
  61. /**
  62. * Instantiates a new mT polygon.
  63. *
  64. * @param pApplet the applet
  65. * @param vertices the vertices
  66. */
  67. public MTPolygon(PApplet pApplet, Vertex[] vertices) { //Added for consitency
  68. super(pApplet, vertices);
  69. this.normalDirty = true;
  70. // this.hasVertexColor = false;//Dont set here, gets set to false after being true in super constructor
  71. this.setTextureEnabled(false);
  72. this.setTextureMode(PApplet.NORMALIZED);
  73. this.setEnabled(true);
  74. this.setVisible(true);
  75. this.setDrawSmooth(true);
  76. this.setNoStroke(false);
  77. this.setNoFill(false);
  78. this.setName("Polygon");
  79. this.setBoundsBehaviour(AbstractShape.BOUNDS_DONT_USE);
  80. // this.setBoundsBehaviour(AbstractShape.BOUNDS_ONLY_CHECK);
  81. }
  82. /* (non-Javadoc)
  83. * @see org.mt4j.components.visibleComponents.shapes.AbstractShape#computeDefaultBounds()
  84. */
  85. @Override
  86. protected IBoundingShape computeDefaultBounds(){
  87. return new BoundingSphere(this);
  88. }
  89. /* (non-Javadoc)
  90. * @see org.mt4j.components.visibleComponents.shapes.AbstractShape#setGeometryInfo(org.mt4j.components.visibleComponents.GeometryInfo)
  91. */
  92. @Override
  93. public void setGeometryInfo(GeometryInfo geometryInfo) {
  94. super.setGeometryInfo(geometryInfo);
  95. this.normalDirty = true;
  96. //FIXME TEST
  97. //If we use processings drawing we have to check if the geometry has individually colored vertices
  98. if (!MT4jSettings.getInstance().isOpenGlMode() || (MT4jSettings.getInstance().isOpenGlMode() && this.isUseDirectGL())){
  99. this.hasVertexColor = this.hasVertexColors(geometryInfo);
  100. }
  101. }
  102. //FIXME TEST
  103. /** The has vertex color. */
  104. private boolean hasVertexColor;
  105. /**
  106. * Checks for vertex colors.
  107. *
  108. * @param geometryInfo the geometry info
  109. * @return true, if successful
  110. */
  111. private boolean hasVertexColors(GeometryInfo geometryInfo){
  112. Vertex[] verts = geometryInfo.getVertices();
  113. for (Vertex vertex : verts) {
  114. if (vertex.getR() != Vertex.DEFAULT_RED_COLOR_COMPONENT ||
  115. vertex.getG() != Vertex.DEFAULT_GREEN_COLOR_COMPONENT ||
  116. vertex.getB() != Vertex.DEFAULT_BLUE_COLOR_COMPONENT ||
  117. vertex.getA() != Vertex.DEFAULT_ALPHA_COLOR_COMPONENT
  118. ) {
  119. return true;
  120. }
  121. }
  122. return false;
  123. }
  124. /* (non-Javadoc)
  125. * @see org.mt4j.components.visibleComponents.shapes.AbstractShape#setUseDirectGL(boolean)
  126. */
  127. @Override
  128. public void setUseDirectGL(boolean drawPureGL) {
  129. super.setUseDirectGL(drawPureGL);
  130. //If we use processings drawing we have to check if the geometry has individually colored vertices
  131. if (!drawPureGL && !this.hasVertexColor){
  132. this.hasVertexColor = this.hasVertexColors(this.getGeometryInfo());
  133. }
  134. }
  135. /* (non-Javadoc)
  136. * @see org.mt4j.components.visibleComponents.shapes.AbstractShape#setVertices(org.mt4j.util.math.Vertex[])
  137. */
  138. @Override
  139. public void setVertices(Vertex[] vertices) {
  140. super.setVertices(vertices);
  141. this.normalDirty = true;
  142. }
  143. /* (non-Javadoc)
  144. * @see org.mt4j.components.visibleComponents.AbstractVisibleComponent#drawComponent(processing.core.PGraphics)
  145. */
  146. @Override
  147. public void drawComponent(PGraphics g) {
  148. // super.drawComponent(g);
  149. PApplet renderer = this.getRenderer();
  150. //Draw the shape
  151. if (MT4jSettings.getInstance().isOpenGlMode()
  152. && this.isUseDirectGL()){
  153. GL gl = Tools3D.beginGL(renderer);
  154. //Draw with PURE opengl
  155. if (this.isUseDisplayList() /*&& this.getDisplayListIDs() != null && this.getDisplayListIDs()[0] != -1 && this.getDisplayListIDs()[1] != -1*/){
  156. int[] displayLists = this.getGeometryInfo().getDisplayListIDs();
  157. //Use Display Lists
  158. if (!this.isNoFill() && displayLists[0] != -1)
  159. gl.glCallList(displayLists[0]); //Draw fill
  160. if (!this.isNoStroke() && displayLists[1] != -1)
  161. gl.glCallList(displayLists[1]); //Draw outline
  162. }else{
  163. //Use Vertex Arrays or VBOs
  164. this.drawPureGl(gl);
  165. }
  166. Tools3D.endGL(renderer);
  167. }else{ //Draw with pure proccessing commands...
  168. MTColor fillColor = this.getFillColor();
  169. MTColor strokeColor = this.getStrokeColor();
  170. g.fill(fillColor.getR(), fillColor.getG(), fillColor.getB(), fillColor.getAlpha());
  171. g.stroke(strokeColor.getR(), strokeColor.getG(), strokeColor.getB(), strokeColor.getAlpha());
  172. g.strokeWeight(this.getStrokeWeight());
  173. if (MT4jSettings.getInstance().isOpenGlMode())
  174. if (this.isDrawSmooth())
  175. g.smooth();
  176. else
  177. g.noSmooth();
  178. //NOTE: if noFill() and noStroke()->absolutely nothing will be drawn-even when texture is set
  179. if (this.isNoFill())
  180. g.noFill();
  181. if (this.isNoStroke())
  182. g.noStroke();
  183. //Set the tint values
  184. g.tint(fillColor.getR(), fillColor.getG(), fillColor.getB(), fillColor.getAlpha());
  185. //handles the drawing of the vertices with the texture coordinates
  186. //try doing a smoothed poly outline with opengl
  187. if (
  188. MT4jSettings.getInstance().isOpenGlMode() &&
  189. this.isDrawSmooth() &&
  190. !this.isNoStroke() &&
  191. !this.isUseDirectGL()
  192. ){
  193. //draw FILL of polygon, without smooth or stroke
  194. g.noStroke();
  195. g.noSmooth();
  196. drawWithProcessing(g);
  197. // DRAW SMOOTHED THE STROKE outline OF THE POLYGON WIHTOUT FILL OR TEXTURE
  198. g.smooth();
  199. g.noFill();
  200. g.stroke(strokeColor.getR(), strokeColor.getG(), strokeColor.getB(), strokeColor.getAlpha());
  201. drawWithProcessing(g);
  202. g.noSmooth();
  203. // //restore fill color
  204. // g.fill(fillColor.getR(), fillColor.getG(), fillColor.getB(), fillColor.getAlpha());
  205. }else{
  206. drawWithProcessing(g);
  207. }//end if gl and smooth
  208. //reSet the tint values to defaults
  209. g.tint(255, 255, 255, 255);
  210. if (/*MT4jSettings.getInstance().isOpenGlMode() &&*/ this.isDrawSmooth())
  211. g.noSmooth(); //because of tesselation bug/lines visibile in shapes
  212. }
  213. }
  214. /**
  215. * loops through all the vertices of the polygon
  216. * and uses processings "vertex()" command to set their position
  217. * and texture.
  218. * @param g PGraphics
  219. */
  220. protected void drawWithProcessing(PGraphics g){
  221. g.beginShape(PApplet.POLYGON); //TODO make setbeginshape() behavior settable
  222. if (this.getTexture() != null && this.isTextureEnabled()){
  223. g.texture(this.getTexture());
  224. g.textureMode(this.getTextureMode());
  225. }
  226. Vertex[] vertices = this.getVerticesLocal();
  227. for (Vertex v : vertices) {
  228. //FIXME TEST
  229. if (this.hasVertexColor) {
  230. g.fill(v.getR(), v.getG(), v.getB(), v.getA()); //takes vertex colors into account
  231. }
  232. if (this.isTextureEnabled())
  233. g.vertex(v.x, v.y, v.z, v.getTexCoordU(), v.getTexCoordV());
  234. else {
  235. if (v.getType() == Vector3D.BEZIERVERTEX) {
  236. BezierVertex b = (BezierVertex) v;
  237. g.bezierVertex(
  238. b.getFirstCtrlPoint().x, b.getFirstCtrlPoint().y, b.getFirstCtrlPoint().z,
  239. b.getSecondCtrlPoint().x, b.getSecondCtrlPoint().y, b.getSecondCtrlPoint().z,
  240. b.x, b.y, b.z);
  241. } else
  242. g.vertex(v.x, v.y, v.z);
  243. }
  244. }
  245. g.endShape();
  246. }
  247. /*
  248. *TODO
  249. * To do multi-texture:
  250. *
  251. * glClientActiveTexture(GL_TEXTURE1);
  252. * glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  253. * glTexCoordPointer(2, GL_FLOAT, sizeof(myVertex), &myQuad[0].s1);
  254. *
  255. */
  256. /**
  257. * Draws with pure opengl commands (without using processing) using vertex arrays, or vbos for speed.
  258. * It is assumed that PGraphicsOpenGL's beginGL() method has already been called
  259. * before calling this method!
  260. *
  261. * @param gl the gl
  262. */
  263. protected void drawPureGl(GL gl){
  264. // /*
  265. //Get display array/buffer pointers
  266. FloatBuffer tbuff = this.getGeometryInfo().getTexBuff();
  267. FloatBuffer vertBuff = this.getGeometryInfo().getVertBuff();
  268. FloatBuffer colorBuff = this.getGeometryInfo().getColorBuff();
  269. FloatBuffer strokeColBuff = this.getGeometryInfo().getStrokeColBuff();
  270. IntBuffer indexBuff = this.getGeometryInfo().getIndexBuff();
  271. //Enable Pointers, set vertex array pointer
  272. gl.glEnableClientState(GL.GL_VERTEX_ARRAY);
  273. gl.glEnableClientState(GL.GL_COLOR_ARRAY);
  274. if (this.isUseVBOs()){//Vertices
  275. gl.glBindBuffer(GL.GL_ARRAY_BUFFER, this.getGeometryInfo().getVBOVerticesName());
  276. gl.glVertexPointer(3, GL.GL_FLOAT, 0, 0);
  277. }else{
  278. gl.glVertexPointer(3, GL.GL_FLOAT, 0, vertBuff);
  279. }
  280. //Default texture target
  281. int textureTarget = GL.GL_TEXTURE_2D;
  282. /////// DRAW SHAPE ///////
  283. if (!this.isNoFill()){
  284. boolean textureDrawn = false;
  285. if (this.isTextureEnabled()
  286. && this.getTexture() != null
  287. && this.getTexture() instanceof GLTexture) //Bad for performance?
  288. {
  289. GLTexture tex = (GLTexture)this.getTexture();
  290. textureTarget = tex.getTextureTarget();
  291. //tells opengl which texture to reference in following calls from now on!
  292. //the first parameter is eigher GL.GL_TEXTURE_2D or ..1D
  293. gl.glEnable(textureTarget);
  294. gl.glBindTexture(textureTarget, tex.getTextureID());
  295. gl.glEnableClientState(GL.GL_TEXTURE_COORD_ARRAY);
  296. if (this.isUseVBOs()){//Texture
  297. gl.glBindBuffer(GL.GL_ARRAY_BUFFER, this.getGeometryInfo().getVBOTextureName());
  298. gl.glTexCoordPointer(2, GL.GL_FLOAT, 0, 0);
  299. }else
  300. gl.glTexCoordPointer(2, GL.GL_FLOAT, 0, tbuff);
  301. textureDrawn = true;
  302. }
  303. if (this.isUseVBOs()){//Color
  304. gl.glBindBuffer(GL.GL_ARRAY_BUFFER, this.getGeometryInfo().getVBOColorName());
  305. gl.glColorPointer(4, GL.GL_FLOAT, 0, 0);
  306. }else{
  307. gl.glColorPointer(4, GL.GL_FLOAT, 0, colorBuff);
  308. }
  309. //Normals
  310. if (this.getGeometryInfo().isContainsNormals()){
  311. gl.glEnableClientState(GL.GL_NORMAL_ARRAY);
  312. if (this.isUseVBOs()){
  313. gl.glBindBuffer(GL.GL_ARRAY_BUFFER, this.getGeometryInfo().getVBONormalsName());
  314. gl.glNormalPointer(GL.GL_FLOAT, 0, 0);
  315. }else{
  316. gl.glNormalPointer(GL.GL_FLOAT, 0, this.getGeometryInfo().getNormalsBuff());
  317. }
  318. }
  319. //DRAW //Draw with drawElements if geometry is indexed, else draw with drawArrays!
  320. if (this.getGeometryInfo().isIndexed()){
  321. gl.glDrawElements(this.getFillDrawMode(), indexBuff.limit(), GL.GL_UNSIGNED_INT, indexBuff);
  322. // gl.glDrawElements(this.getFillDrawMode(), indexBuff.capacity(), GL.GL_UNSIGNED_INT, indexBuff);
  323. }else{
  324. gl.glDrawArrays(this.getFillDrawMode(), 0, vertBuff.capacity()/3);
  325. }
  326. if (this.getGeometryInfo().isContainsNormals()){
  327. gl.glDisableClientState(GL.GL_NORMAL_ARRAY);
  328. }
  329. if (textureDrawn){
  330. gl.glBindTexture(textureTarget, 0);//Unbind texture
  331. gl.glDisableClientState(GL.GL_TEXTURE_COORD_ARRAY);
  332. gl.glDisable(textureTarget); //weiter nach unten?
  333. }
  334. }
  335. ////////// DRAW OUTLINE ////////
  336. if (!this.isNoStroke()){
  337. if (this.isUseVBOs()){
  338. gl.glBindBuffer(GL.GL_ARRAY_BUFFER, this.getGeometryInfo().getVBOStrokeColorName());
  339. gl.glColorPointer(4, GL.GL_FLOAT, 0, 0);
  340. }else{
  341. gl.glColorPointer(4, GL.GL_FLOAT, 0, strokeColBuff);
  342. }
  343. // //Turn on smooth outlines
  344. // if (this.isDrawSmooth()){
  345. // gl.glEnable(GL.GL_LINE_SMOOTH);
  346. // }
  347. //FIXME TEST
  348. Tools3D.setLineSmoothEnabled(gl, true);
  349. // /*
  350. //SET LINE STIPPLE
  351. short lineStipple = this.getLineStipple();
  352. if (lineStipple != 0){
  353. gl.glLineStipple(1, lineStipple);
  354. gl.glEnable(GL.GL_LINE_STIPPLE);
  355. }
  356. // */
  357. if (this.getStrokeWeight() > 0)
  358. gl.glLineWidth(this.getStrokeWeight());
  359. //DRAW Polygon outline
  360. //Draw with drawElements if geometry is indexed, else draw with drawArrays!
  361. if (this.getGeometryInfo().isIndexed()){
  362. gl.glDrawElements(GL.GL_LINE_STRIP, indexBuff.limit(), GL.GL_UNSIGNED_INT, indexBuff);
  363. // gl.glDrawElements(this.getFillDrawMode(), indexBuff.capacity(), GL.GL_UNSIGNED_INT, indexBuff);
  364. }else{
  365. gl.glDrawArrays(GL.GL_LINE_STRIP, 0, vertBuff.capacity()/3);
  366. }
  367. //RESET LINE STIPPLE
  368. if (lineStipple != 0){
  369. gl.glDisable(GL.GL_LINE_STIPPLE);
  370. }
  371. //FIXME TEST
  372. Tools3D.setLineSmoothEnabled(gl, false);
  373. /*
  374. if (this.isDrawSmooth())
  375. gl.glDisable(GL.GL_LINE_SMOOTH);
  376. */
  377. }
  378. gl.glDisableClientState(GL.GL_VERTEX_ARRAY);
  379. gl.glDisableClientState(GL.GL_COLOR_ARRAY);
  380. //TEST
  381. if (this.isUseVBOs()){
  382. gl.glBindBuffer(GL.GL_ARRAY_BUFFER, 0);
  383. gl.glBindBuffer(GL.GL_ELEMENT_ARRAY_BUFFER, 0);
  384. }
  385. // */
  386. }
  387. /* (non-Javadoc)
  388. * @see org.mt4j.components.visibleComponents.shapes.AbstractShape#isGeometryContainsPointLocal(org.mt4j.util.math.Vector3D)
  389. */
  390. @Override
  391. public boolean isGeometryContainsPointLocal(Vector3D testPoint) {
  392. return ToolsGeometry.isPolygonContainsPoint(this.getVerticesLocal(), testPoint);
  393. }
  394. /* (non-Javadoc)
  395. * @see org.mt4j.components.visibleComponents.shapes.AbstractShape#getGeometryIntersectionLocal(org.mt4j.util.math.Ray)
  396. */
  397. @Override
  398. public Vector3D getGeometryIntersectionLocal(Ray ray){
  399. Vector3D[] vertices;
  400. vertices = this.getVerticesLocal();
  401. Vector3D polyNormal = this.getNormal();
  402. //Possible intersection point in plane of polygon
  403. Vector3D interSectPoint = ToolsGeometry.getRayPlaneIntersection(ray, polyNormal, vertices[0]);
  404. if (interSectPoint == null)
  405. return null;
  406. return (ToolsGeometry.isPoint3DInPlanarPolygon(vertices, interSectPoint, polyNormal) ? interSectPoint : null);
  407. }
  408. /**
  409. * Returns a normalized vector, perpendicular to the polygon (the normal)<br>
  410. * <br>The normal vector is calculated in local object space! To transform it into
  411. * world space use <code>normal.transformNormal(Matrix worldMatrix);</code>
  412. * <br><b>NOTE:</b> The polygon has to have at least 3 vertices, the Polygon has to be coplanar!
  413. * <br><b>NOTE:</b> Uses the three first vertices for computation, so make sure there arent duplicates!
  414. *
  415. * @return the normal vector
  416. */
  417. public Vector3D getNormal(){
  418. try {
  419. if (normalDirty){
  420. Vertex[] vertices;
  421. vertices = this.getVerticesLocal();
  422. if (vertices[0].equalsVector(vertices[1])
  423. || vertices[0].equalsVector(vertices[2])
  424. ){
  425. System.err.println("Warning: in component " + this.getName() + ", 2 vectors for normal computation are equal -> bad results! -" + this);
  426. }
  427. this.normal = ToolsGeometry.getNormal(vertices[0], vertices[1], vertices[2], true);
  428. this.normalDirty = false;
  429. return this.normal.getCopy();
  430. }else{
  431. return this.normal.getCopy();
  432. }
  433. } catch (Exception e) {
  434. e.printStackTrace();
  435. return new Vector3D(0,0,1);
  436. }
  437. }
  438. /**
  439. * Calculates the area of a 2D polygon using its transformed world coordinates
  440. * <br>NOTE: works only if the last vertex is equal to the first (polygon is closed correctly).
  441. *
  442. * @return the area as double
  443. */
  444. public double get2DPolygonArea(){
  445. return ToolsGeometry.getPolygonArea2D(this.getVerticesGlobal());
  446. }
  447. /**
  448. * Calculates the center of mass of the polygon.
  449. * NOTE: works only if the last vertex is equal to the first (polygon is closed correctly)
  450. * NOTE: polygon needs to be coplanar and in the X,Y plane!
  451. *
  452. * @return the center or mass as a Vector3D
  453. */
  454. public Vector3D getCenterOfMass2DLocal(){
  455. return ToolsGeometry.getPolygonCenterOfMass2D(this.getVerticesLocal());
  456. }
  457. /* (non-Javadoc)
  458. * @see org.mt4j.components.visibleComponents.shapes.AbstractShape#getCenterPointLocal()
  459. */
  460. @Override
  461. public Vector3D getCenterPointLocal(){
  462. if (this.hasBounds()){
  463. return this.getBounds().getCenterPointLocal();
  464. }else{
  465. return new OrientedBoundingBox(this).getCenterPointLocal();
  466. }
  467. }
  468. /**
  469. * Scales this shape to the given width and height. Relative to its parent frame of reference.
  470. * <br>Uses the shapes bounding shape for calculation.
  471. *
  472. * @param width the width
  473. * @param height the height
  474. *
  475. * @return true, if sets the size xy relative to parent
  476. *
  477. * returns false if negative values are put in
  478. */
  479. public boolean setSizeXYRelativeToParent(float width, float height){
  480. if (width > 0 && height > 0){
  481. Vector3D centerPoint = this.getCenterPointRelativeToParent();
  482. this.scale( (1f/this.getWidthXYRelativeToParent()) * width, (1f/this.getHeightXYRelativeToParent()) * height, 1, centerPoint);
  483. return true;
  484. }else
  485. return false;
  486. }
  487. /**
  488. * Scales this shape to the given width and height in the XY-Plane. Relative to world space.
  489. * <br>Uses the shapes bounding shape for calculation.
  490. *
  491. * @param width the width
  492. * @param height the height
  493. *
  494. * @return true, if sets the size xy global
  495. */
  496. public boolean setSizeXYGlobal(float width, float height){
  497. if (width > 0 && height > 0){
  498. Vector3D centerPoint = this.getCenterPointGlobal();
  499. this.scaleGlobal( (1f/this.getWidthXYGlobal())* width , (1f/this.getHeightXYGlobal()) * height, 1, centerPoint);
  500. return true;
  501. }else
  502. return false;
  503. }
  504. /**
  505. * Scales the shape to the given height relative to parent space.
  506. * Aspect ratio is preserved! The scaling is done Axis aligned, so
  507. * shearing might occour if rotated!
  508. * <br>Uses the shapes bounding shape for calculation.
  509. *
  510. * @param height the height
  511. *
  512. * @return true, if the height isnt negative
  513. */
  514. public boolean setHeightXYRelativeToParent(float height){
  515. if (height > 0){
  516. Vector3D centerPoint = this.getCenterPointRelativeToParent();
  517. float factor = (1f/this.getHeightXYRelativeToParent()) * height;
  518. this.scale(factor, factor, 1, centerPoint);
  519. return true;
  520. }else
  521. return false;
  522. }
  523. /**
  524. * Scales the shape to the given height relative to world space.
  525. * Aspect ratio is preserved! The scaling is done Axis aligned, so
  526. * shearing might occour if rotated!
  527. * <br>Uses the shapes bounding shape for calculation.
  528. *
  529. * @param height the height
  530. *
  531. * @return true, if sets the height xy global
  532. */
  533. public boolean setHeightXYGlobal(float height){
  534. if (height > 0){
  535. Vector3D centerPoint = this.getCenterPointGlobal();
  536. float factor = (1f/this.getHeightXYGlobal())* height;
  537. this.scaleGlobal(factor, factor, 1, centerPoint);
  538. return true;
  539. }else
  540. return false;
  541. }
  542. /**
  543. * Scales the shape to the given width relative to parent space.
  544. * Aspect ratio is preserved!
  545. * <br>NOTE: The scaling is done Axis aligned, so
  546. * shearing might occour if rotated before!
  547. * <br>Uses the shapes bounding shape for calculation.
  548. *
  549. * @param width the width
  550. *
  551. * @return true, if the width isnt negative
  552. */
  553. public boolean setWidthXYRelativeToParent(float width){
  554. if (width > 0){
  555. Vector3D centerPoint = this.getCenterPointRelativeToParent();
  556. float factor = (1f/this.getWidthXYRelativeToParent()) * width;
  557. this.scale(factor, factor, 1, centerPoint);
  558. return true;
  559. }else
  560. return false;
  561. }
  562. /**
  563. * Scales the shape to the given width relative to world space.
  564. * Aspect ratio is preserved! The scaling is done Axis aligned, so
  565. * shearing might occour if rotated!
  566. * <br>Uses the shapes bounding shape for calculation.
  567. *
  568. * @param width the width
  569. *
  570. * @return true, if sets the width xy global
  571. */
  572. public boolean setWidthXYGlobal(float width){
  573. if (width > 0){
  574. Vector3D centerPoint = this.getCenterPointGlobal();
  575. float factor = (1f/this.getWidthXYGlobal())* width;
  576. this.scaleGlobal(factor, factor, 1, centerPoint);
  577. return true;
  578. }else
  579. return false;
  580. }
  581. /* (non-Javadoc)
  582. * @see org.mt4j.components.visibleComponents.shapes.AbstractShape#destroyComponent()
  583. */
  584. @Override
  585. protected void destroyComponent() { }
  586. @Override
  587. protected void applyStyleSheetCustom(CSSStyle virtualStyleSheet) {
  588. if (virtualStyleSheet.isModifiedBackgroundImage()) {
  589. getCssHelper().setBackground(this);
  590. }
  591. }
  592. }