I am using linqtoxml to analyse an existing XML file that is basically of the form
<Projects>
<Project ProjectName="name1">
<ObjectType1List>
<ObjectType1 ProjectName="name1" Idx="1">
<Location Top="104" Left="32" Height="64" Wdth="128" />
...
</ObjectType1>
<ObjectType1 ProjectName="name1" Idx="2">
...
</ObjectType1>
</ObjectType1List>
<ObjectType2List>
...
</ObjectType2List>
</Project>
<Project Name ="name2">
</Project>
...
</Project>
Where there are multiple projects in the file and about 10 cannonical object types inside a project – although each project may or may not have all the different object types. But where a project does contain an objecttype, there will be a finite list of instances of that object type.
Now what I am trying to do is to find all objects of a given object type that are positioned on top of each other (and I know that I will only be interested in 2 of the object types, and I am assuming that only objects of the same type will be overlayed on each other). But the objects have been drawn and positioned by hand, so I can’t assume that such objects have exactly the same dimensions or position – but I will assume that they will match within some aribtray maximum size difference, so i have this feeling I need to scan and group objects by some sort of fuzzy requirement based on the center location of an object.
And the “can I have a pony too” request is that I am only interested in seeing more than one object on top of another. Within a list of a given object type, there could be a group of objects positioned on top of each other, as well as a bunch of other objects that do not overlap – I don’t want to know about those latter objects.
So for a file loaded by an XDocument I am thinking of a 2 pass approach like (and I am not sure if I have the correct syntax for Top et al) :
var objects = from object in xdoc.Descendants()
where object.Name = "ObjectType1" or object.Name = "ObjectType2"
select new
{
Project = object.Attribute("ProjectName").Value.ToString(),
ObjectType = object.Name,
Index = (int)object.Attribute("Idx").Value,
Top = (int)object.Element("Location").Attribute("Top").Value,
Left = (int)object.Element("Location").Attribute("Left").Value,
Width = (int)object.Element("Location").Attribute("Width").Value,
Height = (int)object.Element("Location").Attribute("Height").Value
};
var stacked = from object in objects
group object by ???????
Its the ??????? bit that I don’t know how to write. I know I want to group by Project and ObjectType and then by some mathematical function of Top, Left, Width and Height. This is a twofold problem, as I know I don’t understand multiple groupings in linq, and then the comparision of objects will be something like:
abs(obj1.Left + obj1.Width/2 - obj2.Left - obj2.Width/2)<epsilon &&
abs(obj1.Top + obj1.Height/2 - obj2.Top - obj2.Height/2)<epsilon
So any suggestions?
Note that while my current project is .net 3.5 I have no retrictions that would stop me going to 4.0
Edit1
From questions raised in Timwi’s answer. For any object type, I expect that there will be a bunch of objects that will be well located over each other but will not overlap with other objects not in the same bunch. So for objects A, B, C that overlap, You will not have A & B overlap, B & C overlap and A & C not overlapping.
Thus A will always overlap B and C, B will always overlap A & C and C will always overlap A & B. However there may be objects D, E and F which are located elsewhere which do not overlap A, B or C (or each other), but there may be a third (or more) group of objects G, H and I which overlap themselves.
So for a given project, and a given object type, and objects A,B,C (that overlap themselves), D, E and F (that do not overlap with any other object) and G, H and I (which overap themselves and no other objects as well) I’d like to see an output grouped like:
Project
ObjectType1List
Group1
A, B, C
Group2
G, H, I
In the actual data there may be many such groups for a given Object type.
If your input consists entirely of disjoint groups of rectangles, and within each group every rectangle intersects with every other, then you need to compare each rectangle with at most one from each group. Thus:
Since you mentioned you wanted to omit groups that have only single rectangles, you can just filter them out at the end: