views:

2036

answers:

5

Having some Geometry data and a Transform how can the transform be applied to the Geometry to get a new Geometry with it's data transformed ?

Ex: I Have a Path object that has it's Path.Data set to a PathGeometry object, I want to tranform the points of the PathGeometry object in place using a transform, and not apply a transform to the PathGeometry that will be used at render time.

P.S. I know that the Transform class has a method Point Transform.Transform(Point p) that can be used to transform a Point but...is there a way to transform a arbitrary geometry at once?

Edit: See my repply for a currently found solution

A: 

There are two things you have to consider:

  1. Geometry inherits from Freezable, you can't modify the geometry object in-place if it's frozen.
  2. You can scan the PathGeometry list of figures and segments and transform all the points in them but some types, like ArcSegment includes sizes and angles, you can't transform them.
Nir
you can transform anything, even sizes and angles, besides, I know that you can transform points individually, and also my geometry is not frozen. I wanted to know if there's a framework way to apply transformations to an geometry object as a whole.
Pop Catalin
A: 

Unfortunately, I don't think there is a method or property to do what you are asking. At least, I can't find one. (Great question!)

It seems like you would have to do it manually (as you suggest yourself) ... that is call Point Transform.Transform(Point p) for every point in your PathGeometry ... creating a new PathGeometry in the process.

Probably isn't the answer you want. (Rueful Grin)

cplotts
Not sure why this was down voted as it seems the only correct answer.
Goran
+6  A: 

You could try and use Geometry.Combine. It applies a transform during the combine. One catch is that Combine only works if your Geometry has area, so single lines will not work.

Here is a sample that worked for me.

PathGeometry geometry = new PathGeometry();
geometry.Figures.Add(new PathFigure(new Point(10, 10), new PathSegment[] { new LineSegment(new Point(10, 20), true), new LineSegment(new Point(20, 20), true) }, true));
ScaleTransform transform = new ScaleTransform(2, 2);
PathGeometry geometryTransformed = Geometry.Combine(geometry, geometry, GeometryCombineMode.Intersect, transform);
Todd White
Great idea and answer.
cplotts
The only problem is that the combined geometry is not the same as the original geometry. Differences aren't big but might be important.
Goran
+3  A: 

I've found a solution with which arbitrary tranform can be applied to a path geometry, thanks to Todd White's answer:

Basically Geometry.Combine is used to combine the desired geometry with Geometry.Empty using Union, and the desired transform is given. The resulting geometry is transformed with the given transform.

PathGeometry geometryTransformed = Geometry.Combine(Geometry.Empty, geometry, GeometryCombineMode.Union, transform);
Pop Catalin
A: 

Hi, I've had the same problem AND need lines as well (not only geometries with area).

I'm only using PathGeometry, so this may not be the general solution you are looking for, but this worked for me:

this.pathgeom.Transform = transform; PathGeometry transformed = PathGeometry.CreateFromGeometry(this.pathgeom);

Do you actually get the transformed geometry? Applying CreateFromGeometry on a PathGeometry returns the same geometry (same points). You're not transforming the geometry but simply adding the transform property.
Goran