Пример #1
0
 public PartitionNodeElement(int objectId, PartitionNodeElement element)
     : this(objectId)
 {
     PartitionFlags    = element.PartitionFlags;
     FileName          = element.FileName;
     TransformedBBox   = element.TransformedBBox;
     Area              = element.Area;
     VertexCountRange  = element.VertexCountRange;
     NodeCountRange    = element.NodeCountRange;
     PolygonCountRange = element.PolygonCountRange;
     UntransformedBBox = element.UntransformedBBox;
 }
Пример #2
0
        private BaseNodeElement CreateElement(JTNode node)
        {
            //if (!monolithic && node.GeometricSets.Length > 0)
            if (node.ReferencedFile != null)
            {
                PartitionNodeElement partitionElement;

                if (savedFileIds.ContainsKey(node.ID))
                {
                    partitionElement = savedFileIds[node.ID];
                }

                else
                {
                    //var partFileName = String.Join("_", node.Number.Split(Path.GetInvalidFileNameChars())) + "_" + node.ID + ".jt";
                    //var partFileDirectory = Path.Combine(Path.GetDirectoryName(savePath), Path.GetFileNameWithoutExtension(savePath));
                    //var partFilePath = Path.Combine(partFileDirectory, partFileName);

                    //if (!Directory.Exists(partFileDirectory)) Directory.CreateDirectory(partFileDirectory);

                    //partitionElement = new PartitionNodeElement(IdGenUtils.NextId, new JTNode(node) { TransformationMatrix = null }.Save(partFilePath, true, this.separateAttributeSegments))
                    partitionElement = new PartitionNodeElement(IdGenUtils.NextId)
                    {
                        //FileName = new MbString(@".\" + Path.GetFileNameWithoutExtension(savePath) + @"\" + partFileName)
                        FileName = new MbString(node.ReferencedFile)
                    };

                    elements.Add(partitionElement);

                    savedFileIds[node.ID] = partitionElement;
                }

                var instanceElement = new InstanceNodeElement(partitionElement.ObjectId, IdGenUtils.NextId);

                elements.Add(instanceElement);

                ProcessAttributes(new JTNode(node)
                {
                    GeometricSets = null
                }, instanceElement.ObjectId);

                // Process transformatio matrix

                if (node.TransformationMatrix != null)
                {
                    var transformationMatrixAsString = String.Join("|", node.TransformationMatrix);

                    int geometricTransformAttributeElementId;

                    if (uniqueAttributeIds.ContainsKey(transformationMatrixAsString))
                    {
                        geometricTransformAttributeElementId = uniqueAttributeIds[transformationMatrixAsString];
                    }
                    else
                    {
                        geometricTransformAttributeElementId             = IdGenUtils.NextId;
                        uniqueAttributeIds[transformationMatrixAsString] = geometricTransformAttributeElementId;
                        elements.Add(new GeometricTransformAttributeElement(node.TransformationMatrix, geometricTransformAttributeElementId));
                    }

                    instanceElement.AttributeObjectIds.Add(geometricTransformAttributeElementId);
                }

                // END Process transformatio matrix

                return(instanceElement);
            }

            // Process children and store their IDs

            var childNodes         = node.Children;
            var childNodesCount    = childNodes.Count;
            var childNodeObjectIds = new List <int>(childNodesCount);

            for (int i = 0; i < childNodesCount; ++i)
            {
                childNodeObjectIds.Add(CreateElement(childNodes[i]).ObjectId);
            }

            // END Process children and store their IDs



            // Create node

            MetaDataNodeElement nodeElement = node.GeometricSets.Length > 0 ? new PartNodeElement(IdGenUtils.NextId) : new MetaDataNodeElement(IdGenUtils.NextId);

            nodeElement.ChildNodeObjectIds = childNodeObjectIds;

            // END Create node



            // Process transformatio matrix

            if (node.TransformationMatrix != null)
            {
                var transformationMatrixAsString = String.Join("|", node.TransformationMatrix);

                int geometricTransformAttributeElementId;

                if (uniqueAttributeIds.ContainsKey(transformationMatrixAsString))
                {
                    geometricTransformAttributeElementId = uniqueAttributeIds[transformationMatrixAsString];
                }
                else
                {
                    geometricTransformAttributeElementId             = IdGenUtils.NextId;
                    uniqueAttributeIds[transformationMatrixAsString] = geometricTransformAttributeElementId;
                    elements.Add(new GeometricTransformAttributeElement(node.TransformationMatrix, geometricTransformAttributeElementId));
                }

                nodeElement.AttributeObjectIds.Add(geometricTransformAttributeElementId);
            }

            // END Process transformatio matrix



            // Process Geometric Sets

            var geometricSetsCount = node.geometricSets.Length;

            if (geometricSetsCount > 0)
            {
                float x = 0, y = 0, z = 0;
                int   count = 0;

                var groupNodeElement = new GroupNodeElement(IdGenUtils.NextId);
                elements.Add(groupNodeElement);

                for (int i = 0; i < geometricSetsCount; ++i)
                {
                    var geometricSet   = node.GeometricSets[i];
                    var colour         = geometricSet.Colour;
                    var colourAsString = colour.ToString();

                    int materialAttributeElementId;

                    if (uniqueAttributeIds.ContainsKey(colourAsString))
                    {
                        materialAttributeElementId = uniqueAttributeIds[colourAsString];
                    }
                    else
                    {
                        materialAttributeElementId         = IdGenUtils.NextId;
                        uniqueAttributeIds[colourAsString] = materialAttributeElementId;
                        elements.Add(new MaterialAttributeElement(colour, materialAttributeElementId));
                    }

                    var triStripSetShapeNodeElement = new TriStripSetShapeNodeElement(geometricSet, IdGenUtils.NextId)
                    {
                        AttributeObjectIds = new List <int>()
                        {
                            materialAttributeElementId
                        }
                    };

                    elements.Add(triStripSetShapeNodeElement);

                    groupNodeElement.ChildNodeObjectIds.Add(triStripSetShapeNodeElement.ObjectId);

                    x += geometricSet.Center.X;
                    y += geometricSet.Center.Y;
                    z += geometricSet.Center.Z;
                    count++;

                    ProcessAttributes(new JTNode()
                    {
                        GeometricSets = new GeometricSet[] { geometricSet }
                    }, triStripSetShapeNodeElement.ObjectId);
                }

                var rangeLODNodeElement = new RangeLODNodeElement(IdGenUtils.NextId)
                {
                    ChildNodeObjectIds = new List <int>()
                    {
                        groupNodeElement.ObjectId
                    },

                    Center = new CoordF32(x / count, y / count, z / count)
                };

                elements.Add(rangeLODNodeElement);

                nodeElement.ChildNodeObjectIds.Add(rangeLODNodeElement.ObjectId);
            }

            // END Process Geometric Sets



            // Process root element

            if (node == this)
            {
                float area = 0;

                int vertexCountMin = 0,
                    vertexCountMax = 0,

                    nodeCountMin = 0,
                    nodeCountMax = 0,

                    polygonCountMin = 0,
                    polygonCountMax = 0;

                float
                    minX = 0, minY = 0, minZ = 0,
                    maxX = 0, maxY = 0, maxZ = 0;

                bool firstTriStripSetShapeNodeElementVisited = false;

                var triStripSetShapeNodeElementType = typeof(TriStripSetShapeNodeElement);
                var partitionNodeElementType = typeof(PartitionNodeElement);

                for (int elementIndex = 0, elementCount = elements.Count; elementIndex < elementCount; ++elementIndex)
                {
                    var element     = elements[elementIndex];
                    var elementType = element.GetType();

                    if ((monolithic && elementType != triStripSetShapeNodeElementType) || (!monolithic && elementType != partitionNodeElementType))
                    {
                        continue;
                    }

                    CountRange vertexCountRange, nodeCountRange, polygonCountRange;
                    BBoxF32    untransformedBBox;

                    if (monolithic)
                    {
                        var triStripSetShapeNodeElement = (TriStripSetShapeNodeElement)element;

                        area += triStripSetShapeNodeElement.Area;

                        vertexCountRange  = triStripSetShapeNodeElement.VertexCountRange;
                        nodeCountRange    = triStripSetShapeNodeElement.NodeCountRange;
                        polygonCountRange = triStripSetShapeNodeElement.PolygonCountRange;

                        untransformedBBox = triStripSetShapeNodeElement.UntransformedBBox;
                    }

                    else
                    {
                        var childPartitionNodeElement = (PartitionNodeElement)element;

                        area += childPartitionNodeElement.Area;

                        vertexCountRange  = childPartitionNodeElement.VertexCountRange;
                        nodeCountRange    = childPartitionNodeElement.NodeCountRange;
                        polygonCountRange = childPartitionNodeElement.PolygonCountRange;

                        untransformedBBox = childPartitionNodeElement.UntransformedBBox;
                    }

                    vertexCountMin += vertexCountRange.Min;
                    vertexCountMax += vertexCountRange.Max;

                    nodeCountMin += nodeCountRange.Min;
                    nodeCountMax += nodeCountRange.Max;

                    polygonCountMin += polygonCountRange.Min;
                    polygonCountMax += polygonCountRange.Max;

                    var minCorner = untransformedBBox.MinCorner;
                    var maxCorner = untransformedBBox.MaxCorner;

                    if (!firstTriStripSetShapeNodeElementVisited)
                    {
                        minX = minCorner.X;
                        minY = minCorner.Y;
                        minZ = minCorner.Z;

                        maxX = maxCorner.X;
                        maxY = maxCorner.Y;
                        maxZ = maxCorner.Z;

                        firstTriStripSetShapeNodeElementVisited = true;
                    }

                    else
                    {
                        if (minCorner.X < minX)
                        {
                            minX = minCorner.X;
                        }
                        if (minCorner.Y < minY)
                        {
                            minY = minCorner.Y;
                        }
                        if (minCorner.Z < minZ)
                        {
                            minZ = minCorner.Z;
                        }

                        if (maxCorner.X > maxX)
                        {
                            maxX = maxCorner.X;
                        }
                        if (maxCorner.Y > maxY)
                        {
                            maxY = maxCorner.Y;
                        }
                        if (maxCorner.Z > maxZ)
                        {
                            maxZ = maxCorner.Z;
                        }
                    }
                }

                var partitionNodeElement = new PartitionNodeElement(IdGenUtils.NextId)
                {
                    ChildNodeObjectIds = new List <int>()
                    {
                        nodeElement.ObjectId
                    },

                    Area              = area,
                    VertexCountRange  = new CountRange(vertexCountMin, vertexCountMax),
                    NodeCountRange    = new CountRange(nodeCountMin, nodeCountMax),
                    PolygonCountRange = new CountRange(polygonCountMin, polygonCountMax),
                    UntransformedBBox = new BBoxF32(minX, minY, minZ, maxX, maxY, maxZ)
                };

                elements.Insert(0, partitionNodeElement);

                // ProcessAttributes(node, partitionNodeElement.ObjectId); // !!!!!!! Causes double top node !!!!!
            }

            // END Process root element

            elements.Add(nodeElement);

            ProcessAttributes(node, nodeElement.ObjectId);

            return(nodeElement);
        }