GeometryGroup.Bounds Performance

August 4, 2009 Mihai Leave a comment

GeometryGroup.Bounds Performace

GeometryGroup.Bounds Performace

I recently worked with GeometryGroup and I noticed the performance impact when using GeometryGroup.Bounds.

For simple geometries, everything seams to work smoothly, but when adding, let’s say … 100 000 geometries to the GeometryGroup, accessing its Bounds member takes a looong time … approx. 3 seconds (on my dev machine).

Needless to say, such a low performance makes the method unusable in most scenarios.

As a workaround for this issue, I found that using Rect class and its Union method to add the Bounds of each piece of Geometry works faster.

10 times faster.

Example:

//Execution time > 3 seconds

GeometryGroup group = new GeometryGroup();
Random random = new Random();
for (int i = 0; i < 100000; i++)
{
RectangleGeometry rectGeometry = new RectangleGeometry(new Rect(random.Next(), random.Next(), random.Next(), random.Next()));
group.Children.Add(rectGeometry);
}

Rect groupBounds = group.Bounds;

//Execution time < 0.3 seconds

GeometryGroup group = new GeometryGroup();
Random random = new Random();
Rect rectBounds= Rect.Empty;
for (int i = 0; i < 100000; i++)
{
RectangleGeometry rectGeometry = new RectangleGeometry(new Rect(random.Next(), random.Next(), random.Next(), random.Next()));
group.Children.Add(rectGeometry);
rectBounds.Union(rectGeometry.Bounds);
}

Rect groupBounds = rectBounds;

Visual Studio 2010 install experience

June 10, 2009 Mihai Leave a comment

Yesterday I’ve installed VS 2010 on my home computer.

Besides the numerous new features I’ve been amazed by the new web installer for Team System which is 5 MB in size, loads in seconds and looks like this.

Visual Studio 2010 web installer progress screen

Visual Studio 2010 web installer progress screen

The installer quietly downloads and installs all the selected features.

It is definitely the best install experience I ever had with a Microsoft product.

I didn’t have time to explore all the new features but so far I discovered some of the code editor tip and trick that will definitely make any programmer’s life easier.

Check out this video on channel9 which talks about the new code editor in Visual Studio 2010 http://channel9.msdn.com/shows/10-4/10-4-Episode-5-Code-Focused-in-Visual-Studio-2010/

Categories: Programming Tags: ,

C# 3.0 Design Patterns – mistakes in examples

June 10, 2009 Mihai Leave a comment

I recently decided to refresh and extend my knowledge about design patterns.

I chose to read  C# 3.0 Design Patterns by Judith Bishop mostly because of the promise of having every example in C# 3.0.

C# 3.0 Design Patterns

C# 3.0 Design Patterns

It is actually a good book, with comprehensive examples and UML diagrams of 23 design patterns.

Because of the busy schedule, I barely have time for reading, but in the morning, when I go to work, the 30 minutes bus ride seems the perfect time for reading.

This morning I was very happy because I found a mistake in one of the examples. Not really something obvious or something I could bet I’m right (I had to double check it by running the example), but I was able to notice some really important bug in the code and the expected output. I would even say, a priority 0 bug, given the fact that the whole example was trying to prove some behavior in a wrong way.

Don’t get me wrong, I’m not criticizing, I’m just pointing out, that even at this level, mistakes can be done, and I’m able to spot them :)

Categories: Advanced, Books, C#, Programming

Where is SetBinding?

June 3, 2009 Mihai Leave a comment

While you’re programmatically creating binding in WPF you’ve probably used objectName.SetBinding quite a lot.

Having these controls

<Slider Grid.Column=”1″ Minimum=”1″ Maximum=”5″  Grid.Row=”1″ Margin=”5″ Name=”slider2″ SmallChange=”1″ IsSnapToTickEnabled=”True” />
<TextBox Height=”23″ Margin=”5″ Grid.Row=”1″ Name=”textBox2″ VerticalAlignment=”Top” />

you can create a binding between the slider2’s Value and the textBox2’s  Text with the following code

Binding sliderTextBoxBinding = new Binding();
sliderTextBoxBinding.Source = slider2;
sliderTextBoxBinding.Path = new PropertyPath(“Value”);
textBox2.SetBinding(TextBox.TextProperty, sliderTextBoxBinding);

Fairly simple ha?

But what happens when the SetBinding method is simple not there?!

Take this for example:

Having the following controls

<Slider Grid.Column=”1″ Minimum=”1″  Maximum=”5″  Grid.Row=”2″ Height=”30″ VerticalAlignment=”Top” Margin=”5″ Name=”slider3″ SmallChange=”1″ />
<Rectangle Grid.Row=”2″ Height=”10″ Margin=”50″ Name=”rectangle1″ Stroke=”Black” VerticalAlignment=”Top” Fill=”Gray” />

let’s say you want to bind the rectangle1’s ScaleY property of a ScaleTransform applied as a RenderTransform to the slider3’s Value

First of all, you can’t directly bind the two together.

You have to create a ScaleTransform and assign it to the rectangle1’s RenderTransform

ScaleTransform transform = new ScaleTransform();
rectangle1.RenderTransform = transform;

When you modify the transform object the changes will automatically propagate to rectangle1 and you will notice the changes.

Now let’s bind this transform to the slider3 Value.

Binding transformBinding = new Binding();
transformBinding.Source = slider3;
transformBinding.Path = new PropertyPath(“Value”);

transform.SetBinding(ScaleTransform.ScaleYProperty, transformBinding);

But wait! transform has no SetBinding method

Well, in this case, use BindingOperations.SetBinding

“BindingOperations.SetBinding creates and associates a new instance of  BindingExpressionBase with the specified binding target property”. You can find more details here.

So our code now becomes

BindingOperations.SetBinding(transform, ScaleTransform.ScaleYProperty, transformBinding);

Here you have links to to the Source code and the binaries.