Page 1 of 1

polygons with alpha

Posted: Tue Feb 04, 2014 10:53 am
by juergen
Hi,
I play a little bit with LC to check if it satisfied our needs.
My plan is to get a view with XYZ positions and intensity data that should be represented with different colors. To get a view inside I have to use trancaprency colors. At the moment I think it's best to use Polygon3D.
It looks like this:
Image

It looks not bad and it's quite fast. (52020 polygons)

But if I rotate the scene it looks ugly:
Image

It's some time ago I programmed directx but is it a problem of the backface culling or the zOrder of the polygons?
If I swap the polygons and create them from back to front I can see it correctly from back side?

Thanks,
Jürgen

Re: polygons with alpha

Posted: Tue Feb 04, 2014 11:48 am
by ArctionPasi
The polygons are rendered in the order they appear in the View3D.Polygons collection. Alpha test is made vs. already rendered items in the backbuffer. Reversing the polygons order in the collection is most likely needed to see it correctly from behind.

Re: polygons with alpha

Posted: Tue Feb 04, 2014 12:20 pm
by juergen
So I have to add a sorting algorythm to sort the polygons depending on the distance of the camera position.
Creating the scene completely new each time takes to much time.

Re: polygons with alpha

Posted: Tue Feb 04, 2014 12:40 pm
by ArctionPasi
The 3D polygon items can be as they are, just sorting the list should be enough.
By quickly thinking, sorting by camera distance is needed when
- camera rotates (View3D.CameraViewChanged)
- when axis range changes, (axis.ValueRanged)
- Dimensions change (View3D.DimensionsChanged).

Don't sort it again every time you set new data. I believe your data is kind of cubic, MxNxL and therefor all the Polygons are pretty much in their positions. Setting new polygon colors should be enough.

The Polygons collection is a BindingList, which means every time your sort an item, it will cause lot of overhead for chart. That can be avoided.

Create a regular List<Polygon3D> yourPolygonList = new List<Polygon3D>(count);
Fill it from your polygon objects. Then sort that list. Assign the sorted list then like this:
chart.View3D.Polygons = new BindingList<Polygon3D>(yourPolygonList);

When you calculate the distance from camera, avoid taking square root which is an expensive operation. Because absolute distance is not needed, comparison can be made without it.

Re: polygons with alpha

Posted: Tue Feb 04, 2014 2:01 pm
by juergen
I do it now with

Code: Select all

List<Polygon3D> list = m_chart.View3D.Polygons.ToList();
list.Sort(ComparePolygons);
owner.Polygons = new System.ComponentModel.BindingList<Polygon3D>(list); 
I compare absolute positions from a one time calculated camera position with the center of the polygons with this:

Code: Select all

static int ComparePolygons(Polygon3D poly1, Polygon3D poly2)
{
    if (Owner == null)
        return 0;

    Vector3D vec1 = new Vector3D((poly1.Points[0].X + poly1.Points[2].X) / 2, (poly1.YMax + poly1.YMin) / 2, (poly1.Points[0].Z + poly1.Points[1].Z) / 2);
    Vector3D vec2 = new Vector3D((poly2.Points[0].X + poly2.Points[2].X) / 2, (poly2.YMax + poly2.YMin) / 2, (poly2.Points[0].Z + poly2.Points[1].Z) / 2);
    Vector3D diff1 = Vector3D.Subtract(camPosition, vec1);
    Vector3D diff2 = Vector3D.Subtract(camPosition, vec2);
    if (diff1.Length == diff2.Length)
        return 0;

    if (diff1.Length < diff2.Length)
        return -1;
    else
        return 1;
}
It works like a charm.

Thank you ...

Re: polygons with alpha

Posted: Tue Feb 04, 2014 3:30 pm
by ArctionPasi
This code seems to have a small problem. The polygon points are in axis values, not 3D world coordinates. It may look like it works when you have a lot of data there, but I'd advice to use the following method for comparison instead. It converts the polygon points to 3D world coordinates before comparison.

Code: Select all


PointDouble3D p1 = m_chart.View3D.ConvertSeriesValueTo3DWorldCoord(poly1, (poly1.Points[0].X + poly1.Points[2].X) / 2, (poly1.YMax + poly1.YMin) / 2, (poly1.Points[0].Z + poly1.Points[1].Z) / 2);

PointDouble3D p2 = m_chart.View3D.ConvertSeriesValueTo3DWorldCoord(poly2, (poly2.Points[0].X + poly2.Points[2].X) / 2, (poly2.YMax + poly2.YMin) / 2, (poly2.Points[0].Z + poly2.Points[1].Z) / 2);

double dDistSquared1 = (camPosition.X - p1.X) * (camPosition.X - p1.X) + (camPosition.Y - p1.Y) * (camPosition.Y - p1.Y) + (camPosition.Z - p1.Z) * (camPosition.Z - p1.Z);
double dDistSquared2 = (camPosition.X - p2.X) * (camPosition.X - p2.X) + (camPosition.Y - p2.Y) * (camPosition.Y - p2.Y) + (camPosition.Z - p2.Z) * (camPosition.Z - p2.Z);

if (dDistSquared1 == dDistSquared2)
                return 0;
if (dDistSquared1 < dDistSquared2)
                return -1;
return 1; 

Re: polygons with alpha

Posted: Wed Feb 05, 2014 10:40 am
by juergen
ConvertSeriesValueTo3DWorldCoord needs SeriesBase3D as first parameter.
Is there another chance to get World Coordinates in a simple way?

Re: polygons with alpha

Posted: Wed Feb 05, 2014 10:55 am
by ArctionPasi
So it seems, sorry about that. Polygon3D doesn't inherit from SeriesBase3D.

You can create a dummy/invisible/empty PointLineSeries3D object when you create your chart, and pass that for all ConvertSeriesValueTo3DWorldCoord calls. I think there's no simpler way.

Re: polygons with alpha

Posted: Wed Feb 05, 2014 11:21 am
by juergen
It works perfectly.
Additionally I reduced the polygons by checking the trancparency level. If it's to high it makes no sense to display it.
Now it looks nice.

Thanks a lot ...