|
|
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object net.algart.drawing3d.Drawer3D
public abstract class Drawer3D extends java.lang.Object implements Pixel3DDrawer
Universal 3D drawer: the main drawing tool of this toolkit.
This class operates with with some sets of shaping rules
,
coloring rules
and drawing rules
,
which are usually passed to a constructor or an instantiation method.
Also, this class implements Pixel3DDrawer
interface and supports
the current coordinate system
and the current zcut
limit.
It allows this class to provide a very common method
draw(java.util.Collection)
— the main method of this class, which draws
a collection of absolutely random objects.
This class draws objects inside some fixed area of the real plane 0≤x≤screenWidth
,
0≤y≤screenHeight
, named the screen. This screen is generally virtual,
but usually it corresponds to a part of the real user's screen, maybe, with some scaling.
This virtual screen defines the system of screen coordinates, in terms of DrawingRule
interface.
Usually the screen sizes (screenWidth and screenHeight) are passed to a constructor
or an instantiation method and cannot be changed in future.
In terms of Pixel3DDrawer.drawPoint(int, int, double, double, double, double, Color)
method,
implemented by this class, the screen sizes are called "the sizes of the resulting image".
The final graphic result can be obtained in a form of BufferedImage object by
getImage()
method. The sizes of this image can differ from the sizes of the virtual screen.
For example, SimpleDrawer3D
implementation automatically compresses the image, built by
draw(java.util.Collection)
method, in SimpleDrawer3D.getAntialiasingScale()
times:
it allows to provide better quality of the resulting picture.
This class is abstract and does not provide a real implementation of Pixel3DDrawer
interface.
This package offers a simple full implementation in SimpleDrawer3D
class,
based on the traditional Z-buffer algorithm.
This class is not thread-safe, but is thread-compatible and can be synchronized manually if multithread access is necessary.
AlgART Laboratory 2010
Modifier and Type | Field and Description |
---|---|
protected int |
screenHeight
The height of the (virtual) screen, specified in the constructor. |
protected int |
screenWidth
The width of the (virtual) screen, specified in the constructor. |
protected double |
zCut
The current zcut limit, which will be passed to DrawingRule.draw
method by draw(java.util.Collection) method of this class. |
Modifier | Constructor and Description |
---|---|
protected |
Drawer3D(int screenWidth,
int screenHeight,
java.awt.Color backgroundColor,
java.util.Collection<ShapingRule> shapingRules,
java.util.Collection<ColoringRule> coloringRules,
java.util.Collection<DrawingRule> drawingRules)
Creates new instance of this class. |
Modifier and Type | Method and Description |
---|---|
abstract void |
clearRect(int left,
int top,
int width,
int height)
Clears (reinitialize) the rectangular area left≤x<left+width, top≤y<top+height. |
void |
draw(java.util.Collection<?> items)
Draws all passed objects. |
void |
draw(java.lang.Object... items)
Draws all passed objects. |
abstract void |
drawPoint(int x,
int y,
double z,
double nx,
double ny,
double nz,
java.awt.Color color)
Draws 1 pixel (point) of the surface of some 3D figure. |
boolean |
estimateContainingParallelepiped(double[] result,
java.util.Collection<?> items)
Returns, in first 6 elements of result argument, the minimal and maximal x-coordinate, the minimal and maximal y-coordinate, the minimal and maximal z-coordinate of all drawable points of all passed 3D objects, in the specified order. |
protected int |
gammaCorrection(int brightness)
Returns (int)(256.0 * (brightness/256.0)γ) + 0.5), where γ is the current γ exponent for the gamma-correction. |
java.awt.Color |
getBackgroundColor()
Returns the current background color, used by clearRect(int, int, int, int) method. |
java.awt.Color |
getColor(java.lang.Object item)
Finds and returns the desired color of the given object. |
java.util.List<ColoringRule> |
getColoringRules()
Returns the list of coloring rules, used by this object. |
CoordinateSystem3D |
getCoordinateSystem()
Returns the current coordinate system. |
DrawingRule |
getDrawingRule(Shape3D shape,
java.lang.Object item)
Finds and returns the drawing rule, which should be used for drawing the given 3D figure. |
java.util.List<DrawingRule> |
getDrawingRules()
Returns the list of drawing rules, used by this object. |
double |
getGamma()
Returns the current γ, used for gamma-correction of the pixel brightness. |
abstract java.awt.image.BufferedImage |
getImage()
Transforms the current picture, built on the virtual screen by this object, into a buffered image, and returns it. |
abstract int |
getImageHeight()
Returns the height of the image, built by getImage() method. |
abstract int |
getImageWidth()
Returns the width of the image, built by getImage() method. |
int |
getScreenHeight()
Returns the height of the (virtual) screen: screenHeight . |
int |
getScreenWidth()
Returns the width of the (virtual) screen: screenWidth . |
Shape3D |
getShape(java.lang.Object item)
Finds and returns the 3D shape (figure) of the given object. |
java.util.List<ShapingRule> |
getShapingRules()
Returns the list of shaping rules, used by this object. |
double |
getZCut()
Returns the current zcut limit. |
void |
setBackgroundColor(java.awt.Color backgroundColor)
Sets the new current background color, used by clearRect(int, int, int, int) method. |
void |
setColoringRules(java.util.Collection<ColoringRule> coloringRules)
Sets the new set of coloring rules, used by this object. |
void |
setColoringRules(ColoringRule... coloringRules)
Sets the new set of coloring rules, used by this object. |
void |
setCoordinateSystem(CoordinateSystem3D coordinateSystem)
Sets the new current coordinate system. |
void |
setDrawingRules(java.util.Collection<DrawingRule> drawingRules)
Sets the new set of drawing rules, used by this object. |
void |
setDrawingRules(DrawingRule... drawingRules)
Sets the new set of drawing rules, used by this object. |
void |
setGamma(double gamma)
Sets the current γ, used for gamma-correction of the pixel brightness. |
void |
setShapingRules(java.util.Collection<ShapingRule> shapingRules)
Sets the new set of shaping rules, used by this object. |
void |
setShapingRules(ShapingRule... shapingRules)
Sets the new set of shaping rules, used by this object. |
void |
setZCut(double zCut)
Sets the new current zcut limit. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
protected final int screenWidth
protected final int screenHeight
protected double zCut
DrawingRule.draw
method by draw(java.util.Collection)
method of this class.
By default, it is Double.POSITIVE_INFINITY, but it can be set to another value
by setZCut(double)
method.
Constructor Detail |
---|
protected Drawer3D(int screenWidth, int screenHeight, java.awt.Color backgroundColor, java.util.Collection<ShapingRule> shapingRules, java.util.Collection<ColoringRule> coloringRules, java.util.Collection<DrawingRule> drawingRules)
The passed collections are copied into newly created lists: no references to them are maintained by the created object.
screenWidth
- the width of the (virtual) screen: screenWidth
. Cannot be changed in future.screenHeight
- the height of the (virtual) screen: screenHeight
. Cannot be changed in future.backgroundColor
- the background color
,
which will be used by clearRect(int, int, int, int)
method.
After creation, all screen is filled by this color.shapingRules
- the set of shaping rules, which will be used
by draw
method.coloringRules
- the set of coloring rules, which will be used
by draw
method.drawingRules
- the set of drawing rules, which will be used
by draw
method.java.lang.NullPointerException
- if some of the arguments is null, or
if one of rules in the passed collections is null.java.lang.IllegalArgumentException
- if screenWidth<0 or screenHeight<0.Method Detail |
---|
public java.util.List<ShapingRule> getShapingRules()
The result is an immutable view (Collections.unmodifiableList) of the internal collection, stored by this object: you cannot modify the set of used shaping rules via the result of this method.
The elements of the returned list are never null.
public void setShapingRules(java.util.Collection<ShapingRule> shapingRules)
The passed collection is copied into a newly created list: no references to it are maintained by this object.
shapingRules
- new shaping rules.java.lang.NullPointerException
- if the argument or one the elements of the passed collection is null.public void setShapingRules(ShapingRule... shapingRules)
The passed array is copied into a newly created list: no references to it are maintained by this object.
shapingRules
- new shaping rules.java.lang.NullPointerException
- if the argument or one the elements of the passed collection is null.public java.util.List<ColoringRule> getColoringRules()
The result is an immutable view (Collections.unmodifiableList) of the internal collection, stored by this object: you cannot modify the set of used coloring rules via the result of this method.
The elements of the returned list are never null.
public void setColoringRules(java.util.Collection<ColoringRule> coloringRules)
The passed collection is copied into a newly created list: no references to it are maintained by this object.
coloringRules
- new coloring rules.java.lang.NullPointerException
- if the argument or one the elements of the passed collection is null.public void setColoringRules(ColoringRule... coloringRules)
The passed array is copied into a newly created list: no references to it are maintained by this object.
coloringRules
- new coloring rules.java.lang.NullPointerException
- if the argument or one the elements of the passed collection is null.public java.util.List<DrawingRule> getDrawingRules()
The result is an immutable view (Collections.unmodifiableList) of the internal collection, stored by this object: you cannot modify the set of used drawing rules via the result of this method.
The elements of the returned list are never null.
public void setDrawingRules(java.util.Collection<DrawingRule> drawingRules)
The passed collection is copied into a newly created list: no references to it are maintained by this object.
drawingRules
- new drawing rules.java.lang.NullPointerException
- if the argument or one the elements of the passed collection is null.public void setDrawingRules(DrawingRule... drawingRules)
The passed array is copied into a newly created list: no references to it are maintained by this object.
drawingRules
- new drawing rules.java.lang.NullPointerException
- if the argument or one the elements of the passed collection is null.public java.awt.Color getBackgroundColor()
clearRect(int, int, int, int)
method.
public void setBackgroundColor(java.awt.Color backgroundColor)
clearRect(int, int, int, int)
method.
backgroundColor
- new current background colorjava.lang.NullPointerException
- if the argument is null.public CoordinateSystem3D getCoordinateSystem()
CoordinateSystem3D
(0.0, 0.0, 0.0, 1.0, Orientation3D.DEFAULT
);The result is a clone (CoordinateSystem3D.clone()
) of
the internal field of this object: you cannot modify the coordinate system
via the result of this method.
public void setCoordinateSystem(CoordinateSystem3D coordinateSystem)
The passed object is cloned (CoordinateSystem3D.clone()
):
no references to it are maintained by this object.
coordinateSystem
- new current coordinate system.java.lang.NullPointerException
- if the argument is null.public double getZCut()
zcut
limit.
public void setZCut(double zCut)
zcut
limit.
zCut
- new current zcut limit.public double getGamma()
setGamma(double)
for more details.
After creating this object, γ=1.0 (no correction).
gammaCorrection(int)
public void setGamma(double gamma)
drawPoint
)
is automatically replaces with Lγ by drawPoint
method.
In many situation, it can improve visual quality of the resulting image.
This method corrects an internal table, used by gammaCorrection(int)
method.
gamma
- the new current γ, used for gamma-correction of the pixel brightness.java.lang.IllegalArgumentException
- if the passed gamma<=0.0.public final int getScreenWidth()
screenWidth
.
public final int getScreenHeight()
screenHeight
.
public abstract int getImageWidth()
getImage()
method.
getImage()
method.public abstract int getImageHeight()
getImage()
method.
getImage()
method.public abstract void clearRect(int left, int top, int width, int height)
Pixel3DDrawer
drawPoint
was never called for integer pairs (x,y) in this area,
and this area is filled by some default background color.
The implementation, provided by
SimpleDrawer3D.getSimpleDrawer3D
method, initializes Z-buffer
for this area by default −∞ values.
If the specified area is out of ranges of the screen (i.e. some coordinates are <0 or ≥width/height of the image, which should be built as a result), the extra pixels are ignored. In particular, you can specify left=top=0, width=height=MAX_VALUE to reinitialize all drawable area. If width≤0 or height≤0, this method does nothing.
clearRect
in interface Pixel3DDrawer
left
- the minimal x-coordinate of the cleared area.top
- the minimal y-coordinate of the cleared area.width
- the width of the cleared area.height
- the height of the cleared area.public abstract void drawPoint(int x, int y, double z, double nx, double ny, double nz, java.awt.Color color)
Pixel3DDrawer
DrawingRule.draw
method is implemented.
The coordinates of the pixel are specified in the coordinate system of the computer screen: the x-axis is directed to the right (in the screen plane), the y-axis is directed downwards (in the screen plane) and the z-axis is directed from the screen to the user. The top left corner of the screen image, which should be built as a result, has coordinates x=0, y=0, z=+∞.
x and y coordinates, passed to this method, specify the pixel of the resulting image,
which should be built (from 0 to the image width−1 or height−1). The resulting color
of the pixel
z-coordinate, passed to this method, is compared with z-coordinates of
other points, which were probably drawn by this method at the same (x,y) coordinates.
The simplest implementation can just overwrite the color of this pixel, if the passed z
is greater than z-coordinates of all previous calls with same
(x,y), or preserve the previous color if not — it is the known "Z-buffer" algorithm.
But other implementations are also possible: for example, the new color can be combined
with the previous color at (x,y) position, to provide an effect of translucency.
The implementation, provided by
SimpleDrawer3D.getSimpleDrawer3D
method, uses the described
simple Z-buffer algorithm.
The color argument specifies the color of the drawn point of the 3D surface.
But this color is used directly only in a case, when the surface is perpendicular to the rays of the light.
In other case, the color should be made darker. To provide this behavior, this method has additional arguments
nx, ny, nz, equal to the components of the unit normal vector
SimpleDrawer3D.getSimpleDrawer3D
method, multiplies all
R, G, B components of the color by Math.abs(nz) —
it corresponds to the light rays, falling along z axis from the side of the user (observer).
Note: the normal vector is supposed to be unit, i.e.
The alpha component of the passed color affects the transparency. It is guaranteed,
that if SimpleDrawer3D.getSimpleDrawer3D
method, uses
1–254 values for mixing the passed color with previously drawn color, which partially emulates
translucency. But note that this feature does not really provide correct translucency, because
that implementation is based on a simple Z-buffer algorithm and does nothing if z argument
is less than z-coordinates of the previously drawn pixel.
If x or y arguments are out of ranges of the screen (i.e. <0 or
≥width/height of the image), this method does nothing.
Besides this, the object can support some zcut limit
and ignore calls with z>zcut: it
allows creating "cut sets" of a complex 3D configuration
(this limit is really supported by Drawer3D
class).
drawPoint
in interface Pixel3DDrawer
x
- x-coordinate of the point.y
- y-coordinate of the point.z
- z-coordinate of the point.nx
- x-component of the unit normal vector of the drawn surface at this point.ny
- y-component of the unit normal vector of the drawn surface at this point.nz
- z-component of the unit normal vector of the drawn surface at this point.color
- the color of drawn surface point.public abstract java.awt.image.BufferedImage getImage()
clearRect
and drawPoint
methods of this object,
but some little transformation, like scaling, is possible:
for example, SimpleDrawer3D
implementation automatically compresses the image
in SimpleDrawer3D.getAntialiasingScale()
times.
Further drawing in this object will not affect the returned image, but the further calls of
this method can change it. (For example, SimpleDrawer3D
implementation uses for building
a buffered image the separate memory, allocated only once, and does not reallocate it in this method.)
getImageWidth()
,
getImageHeight()
public Shape3D getShape(java.lang.Object item)
isApplicable
(item)
for all shaping rules
, used by this object,
and if this method returns true for some rule, this method immediately return
shapingRule.getShape
(item).
If isApplicable
(item) returns false for all shaping rules,
but item implements Shape3D
interface itself,
this method returns (Shape3D
)item.
In other cases, and also if item==null, this method returns null.
item
- some 3D object; can be null, then this method returns nullpublic java.awt.Color getColor(java.lang.Object item)
isApplicable
(item)
for all coloring rules
, used by this object,
and if this method returns true for some rule, this method immediately return
coloringRule.getColor
(item).
If isApplicable
(item) returns false for all coloring rules,
and also if item==null, this method returns null.
item
- some 3D object; can be null, then this method returns nullpublic DrawingRule getDrawingRule(Shape3D shape, java.lang.Object item)
isApplicable
(shape, item)drawing rules
, used by this object,
and if this method returns true for some rule, this method immediately return that
drawingRule.
If isApplicable
(shape, item)
shape
- the 3D figure which you want to draw;
can be null, then this method returns false.item
- the source object which you want to draw as the given figure; can be null.public void draw(java.lang.Object... items)
draw
(Arrays.asList(items)).
items
- objects that should be drawn.java.lang.NullPointerException
- if items array is null.public void draw(java.util.Collection<?> items)
More precisely, this method performs the following operations for each element item of the passed collection:
getShape
(item) call;getColor
(item) call;getDrawingRule
(shape, item)draw
(shape, this,
coordinateSystem, screenArea, zCut, color)Drawer3D
instance,
"coordinateSystem" is the current coordinate system
,
"zCut" is the current zcut limit
and "screenArea" is
screenWidth
,screenHeight
)Note: this method allows to draw absolutely any objects, if there are necessary shaping rules,
which allows to transform them to 3D figures, coloring rules, which allows to select their colors,
and drawing rules, which allows to draw the found 3D figures
with the found colors. But if the argument of this method is a collection of 3D figures
(implementing Shape3D
interface), you can go without shaping rules.
items
- objects that should be drawn.java.lang.NullPointerException
- if the argument is null.public boolean estimateContainingParallelepiped(double[] result, java.util.Collection<?> items)
More precisely, this method performs the following operations for each element item of the passed collection:
getShape
(item) call;getDrawingRule
(shape, item)estimateContainingParallelepiped
for the found shape and
the current coordinate system
.The result argument will contains the union of all found parallelepipeds, i.e. an estimation
of the parallelepiped, circumscribed about all passed items configuration.
Usually, this method helps GUI modules to determine a suitable range for changing
current zcut limit
.
The result of this method is true if at least one parallelepiped was really found, i.e. both shape and drawing rule was not null at least for one item. In other case, this method returns false and does not modify the passed result array.
result
- Java-array for storing results.items
- objects that should be drawn.java.lang.NullPointerException
- if one of the arguments is null.java.lang.IndexOutOfBoundsException
- if result.length<6.protected int gammaCorrection(int brightness)
current γ exponent
for the gamma-correction.
This method works very quickly: it just returns the corresponding element of an internal table,
stored in this object and rebuilt by any setGamma(double)
call.
The argument must be in
brightness
- some brightness which should be corrected, in 0..256 range.java.lang.IndexOutOfBoundsException
- if brightness<0 or brightness≥257.
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |