## Saturday, March 01, 2008

### XNA Line Thickness

I've been searching for a method to display a thick line in XNA. There isn't a method in XNA as OpenGL's glLineWidth. So you have to construct you own line from triangles. I've found and tried this method. The line were rounded at each edge, and the whole scene looked pretty impressive. The problem was that my application had to render thousands of lines, and doing rounded edges and indexed triangles wasn't very performant. I wanted something much more simple, without rounded edges, but considering line joints. I developed a really simple and performant method to do this. The idea was to take a line strip (the array of Vector3[]) and build a triangle strip, which is the most performant method for rendering, and store it in a vertex buffer. This method is almost as performant as drawing simple lines!
I think that the code is pretty self explaining:

triangleCount =-2;
private List<VertexPositionColor> GetTriangleStrip(Vector3[] points, float thickness)
{
Vector3 lastPoint = Vector3.Zero;
List<VertexPositionColor> list = new List&ltVertexPositionColor>();
for (int i=0;i<points.Length;i++)
{
if (i == 0) { lastPoint = points[i]; continue; }
//the direction of the current line
Vector3 direction = lastPoint - points[i];
direction.Normalize();
//the perpendiculat to the current line
Vector3 normal = Vector3.Cross(direction, Vector3.UnitZ);
normal.Normalize();
Vector3 p1 = lastPoint + normal * thickness; triangleCount++;
Vector3 p2 = lastPoint - normal * thickness; triangleCount++;
Vector3 p3 = points[i] + normal * thickness; triangleCount++;
Vector3 p4 = points[i] - normal * thickness; triangleCount++;
lastPoint = points[i];
}
return list;
}
Then you just have to store the list in a vertex buffer and render it as a triangle strip with "triangleCount" primirives.

## Friday, February 29, 2008

### Localizing Domain Driven Design?

I've recently read "Domain-Driven Design: Tackling Complexity in the Heart of Software" - Eric Evans, and was pretty excited by the ubiquitous language which everybody should use, the developers, the analysts, the domain experts, etc...

There is a problem though: we - developers tend to use the english language in our code, and the clients and analysts use Romanian language (I'm from Romania). So would there be two ubiquitous languages, should the developers switch to using Romanian in their code (class names, methods...), or should the analytsts switch to english terms?

For example, we are developing a GIS application, so we (the developers) use terms like "feature", "feature type", "feature attribute", "feature store", "layer", "map context", and other words used by all GIS applications and by OGC. I can't imagine
how we should understand each other if we were using the Romaning translations of these words. So, we can't switch the ubiquitous language to Romanian, but I believe that we can't use english terms with our clients either. There is an important thing to mention: there are no domain experts for GIS involved in our project; there are only clients who want to see a map, so we have to define our own ubiquitous language. So what to do?

I'm e developer, so I tend to use english for the ubiquitous language...

## Thursday, February 28, 2008

### XNA application on NVidia Quadro FX 370

I tested today my application, which uses a lot of textured quads to display a GIS map on a NVidia Quadro FX 370. I expected good results, knowing this is a professional graphic card (although it is entry level), and because it had 256 MBytes RAM - a lot of space for my textures. But surprise - the application performance was actually worse than on my onboard Intel 82945G Express!

I didn't know almost nothing about NVidia Quadro, except it is expensive and it is a "professional" card. After some research, I found that NVidia Quadro it has some improvements for CAD applications, like hardware antialiased lines, and stuff like that. So it isn't very good for gaming - and my XNA application could be compared to a game.

I'm not sure what to do next. Maybe move to a vectorial rendering to make use of the Quadro skills, although I have no ideea if XNA has anything to do with these "skills"...

I just wish I had known earlier that our customers would use this graphic board, so they didn't spend their money in vain, when they could have bought a cheap GeForce and have better performance.

I've been working for some time on rendering with XNA GIS data, especialy WMS tiles. I tried to do something like NASA World Wind or Virtual Earth. I downloaded png files from the WMS server and loaded them using Texture2D.FromFile method. Because there were so many textures, I had to develop a method to load/unload them at runtime.

The problem was that Texture2D.FromFile performance was very poor. I did some research, and saw that NASA World Wind caches the textures in a .dds file. Direct Draw Surface format.It had to work better with dds files than with png (which is a compressed format by the way, and of course we expect the decoding to take some time...).

So I tried to convert the received png file into a dds format, in order to increase tile cache access speed when loading/unloading images. I did something like this:

Texture = Texture2D.FromFile(device, "image.png");
Texture.Save("image.dds", ImageFileFormat.Dds);

When calling Texture2D.FromFile(XnaFactory.Device, "image.Dds"), the performance was greatly improved. When you load the texture at first, from the png file, or whatever other format, you can also specify TextureCreationParameters. For example I use
TextureCreationParameters.SurfaceFormat = SurfaceFormat.Dxt1 so the textures will be compressed.

Also, the resulting dds file would give you a good approximation about how much memory would be used in the graphic card for the texture.

## Tuesday, July 24, 2007

### Wix and Team Build (TFS) integration

At our company, I have struggled for hours to automate the setup package in a Team Build Type in Team Foundation Server.
I found several articles on how to integrate Wix with MSBuild, but none were about using Wix with Team Build. In most of the cases, the msbuild looked like this:

<propertygroup>
<toolpath>C:\Program Files\Wix\</toolpath>
<outputname>MyProjectSetup</outputname>
<outputtype>package</outputtype>
</propertygroup>

<itemgroup>
<compile include="MyProject.wxs">
</compile>

<import project="\$(ToolPath)wix.targets">

I added MyProject.wxs (the wix file) in the build type directory and modified TFSBulid.proj and added the above text. The build was not successful of course :) .
After a carefully examining the log files, I noticed that the wix.targets project overrode some tasks and some properties defined in Microsoft.TeamFoundation.Build.targets file. The solution was to change all properties and targets from wix.targets file like this:
from <target name="Compile"> to <target name="WixCompile">, from BuildDependsOn property to WixBuildDependsOn, and so on...
The final step was to call the WixBuild target from AfterDropBuild overriden target.
The final msbuild script looked like this:

<target name="AfterDropBuild">
...
<calltarget targets="WixBuild">
</calltarget>

<propertygroup>
<toolpath>C:\Program Files\Wix\</toolpath>
<outputname>MyProjectSetup</outputname>
<outputtype>package</outputtype>
</propertygroup>

<Itemgroup>
<Wixcompile include="MyProject.wxs">
</ItemGroup>

<import project="\$(ToolPath)wix.targets">

Everything works just fine now, and we are able to include in the continuous integration process the deployment and setup packages consruction.

## Tuesday, February 13, 2007

### Dynamic ASP.NET web service proxy generation

For many times I have been annoyed by the automatic proxy generation of the ASP.NET web services in Visual Studio. I wanted to work in a more “WCF” style (Windows Communication Foundation), as in using “operation contracts” and “data contracts”. Unfortunatelly, there was no way of doing this in asp.net web services. You had to deal with the duplicated code done by the generator, or you had to manually modify and update the proxy. I considered developing a library for dynamic generation of the web service using only the service contract and the address of the service. With WCF, the asp.net web services are somehow obsolete, but I thought it wouldn’t be so bad to publish my solution, since I havent’ found any way to do dynamic web proxy generation.

Here is how you should use the web service:

1. First, write the service contract (the web service interface, and the data transfer

objects). Example:

public interface IWebServiceInterface
{
string HelloWorld();
int GetSum(int a, int b);
UserDetails GetDetails(string firstName,string lastName,int age);
}

2. Then write the service implementation using ASP.NET web services:

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

public class Service : System.Web.Services.WebService, IWebServiceInterface
{

#region IWebServiceInterface Members

[WebMethod]
public string HelloWorld()
{
return "Hello World";
}

[WebMethod]
public int GetSum(int a, int b)
{
return a + b;
}
[WebMethod]
public UserDetails GetDetails(string firstName, string lastName, int age)
{
UserDetails ret= new UserDetails(firstName,lastName,age);
return ret;
}

#endregion

Publish the web service, and in order to use it, create a IWebServiceInterface instance:

IWebServiceInterface dynamicService =
DynamicProxy
.GetProxy<IWebServiceInterface>("http://localhost/TestService1/Service.asmx");

And there you have it – the dynamic generated asp.net web proxy. You don’t have to worry about duplicated code or manual updates anymore. But pay attention, if you intend to use this code, you shouln’t modify the method or the types namespaces, or other details provided by the attributes, because the library uses only the contract to generate the proxy, and not the information from the WSDL. So the proxy presumes that the web service is in the "http://tempuri.org/" namespace, and all methods are decorated with the [WebMethod] attribute, with no parameters, just as they are generated by the visual studio IDE.

The code generation is done using the System.CodeDom namespace. If you have questions don’t hesitate to write me, or post a comment.

## Monday, January 08, 2007

### Castle project - MicroKernel

In the "Team Work” post I have been talking about an architecture based on contract, in other words – an interface. The presenter for example was using the ILocalDataAccessService and the IOnlineService services.
What happens if the current presenter needs another service? We should change the presenter constructor, and all initialization. We could have the same problem as the one I talked about in the previous post, with the Object Mother – we could have to change the code in a lot of places and maybe hundreds of tests. This is one of the many problems which could appear because of a tight coupled architecture. The dependency using constructor injection isn’t enough sometimes, for example a presenter can use a service only in a specific moment, or it can use a service which isn’t available all the time, so the service shouldn’t necessarily be given as a constructor parameter.
The ideal way is to have an infrastructure, a framework which solves all these problems automatically for us, so we shouldn’t worry about a complex service dependency, or service creation.

The first idea is to have a collection of services, which can be retrieved at any time using their names, and/or interface they implement. These would solve our problem with the service we don’t use all the time, or they could be unavailable at a specific time.

IService service = (IService) Kernel[“My service”];

The second idea is to automate the construction of services, if a service A needs to be constructed and it depends on another service - B, the framework should automatically retrieve the service B from the collection and construct the service A. So the services should be instantiated automatically without having to call explicitly the constructors.

If the BService class has a constructor with a AService parameter, the framework should automatically call the constructor with the service “A” added in the first step.

Another problem would be the service “style”: is the service a singleton? Or should a different instance be created for every call of Kernel[“My Service”] ? Or maybe create a service for each thread? These details should also be handled by the framework.

So, we shouldn’t be worried when changing the constructor of a service. The infrastructure should have to worry now. We could also use this framework for a plugin architecture: until now, I have worked at a few applications which used plug-ins, and most of the time, a plugin defined in fact a service. I had to do every time my own manager for these plugins. Some plugins were singletons, some would had to be instantiated for every call, but I had to think how to do all this by myself. I didn’t use a framework which could do all the work for me.

Some days ago I have searched for such a framework and I found exactly what I was looking for. It is called the Castle Project – MicroKernel. I started using this framework, and – until now, I am very pleased with the results.

## Thursday, January 04, 2007

### TDD problems - Object Mother

I started working on a new project some time ago, and decided to work in a TDD style as much as I could. The idea was that there were very vague specifications for the project, resulting in a very “agile” project. So what methodology other than XP Programming and TDD should I adopt, I thought.(If you don't know about TDD (Test Driven Development) read about it on wiki.)

I have had some experience with TDD before, but I usually stopped writing tests when I started to develop the user interface. Although I used the MVP design pattern, so I should have been able to test the presenter using a mock as a view, I think this is a very time consuming process, and sometimes maybe not very useful, especially when the presenter logic is not very complex – it takes data from the object and passes it to the view.

The project I was talking about is a monitoring engine. Used to monitor computers, software applications and other stuff… The business was pretty complex, and there was no user interface, so the TDD was almost a must. I started to work in almost in a perfect TDD style, but after some time I found it very difficult to maintain the unit tests.

The problem was that a small change in the business code determined a lot of changes in the unit tests. It was pretty upsetting when seeing 100 compile errors when changing just the number of parameters of a constructor. And very time consuming. At some point, 50% of my work consisted in maintaining the unit tests. Most of my tests were constructing business objects in order to test them. A small change in the constructor or in the manner the business object was constructed determined as I said a lot of errors in the tests. So my idea was to separate the object construction from the unit tests, and make some kind of object factory, but a smart factory, which could create very complex objects with just a function call, or let me customize the object creation.

I found that this method is in fact a pattern, called “Object Mother”:

“Through a handful of simple method calls, this utility provided a complete, valid, and customizable structure of business objects (think of an invoice: its lines, all related charges, remit to, bill to).

Object Mother starts with the factory pattern, by delivering prefabricated test-ready objects via a simple method call. It moves beyond the realm of the factory by

1. Facilitating the customization of created objects,
2. Providing methods to update the objects during the tests, and
3. If necessary, deleting the object from the database at the completion of the test.

Now, when I change the number of parameters in a constructor, I have to change only the object mother not all my 1000 test units, so I consider this method very helpful.