All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.citygml4j.cityjson.adapter.geometry.builder.TextureBuilder Maven / Gradle / Ivy

There is a newer version: 3.2.4
Show newest version
/*
 * citygml4j - The Open Source Java API for CityGML
 * https://github.com/citygml4j
 *
 * Copyright 2013-2025 Claus Nagel 
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.citygml4j.cityjson.adapter.geometry.builder;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import org.citygml4j.cityjson.adapter.Fields;
import org.citygml4j.cityjson.adapter.appearance.builder.AppearanceBuilder;
import org.citygml4j.cityjson.adapter.appearance.builder.TextureVerticesBuilder;
import org.citygml4j.cityjson.reader.CityJSONBuilderHelper;
import org.citygml4j.core.model.appearance.*;
import org.xmlobjects.gml.model.geometry.primitives.LinearRing;
import org.xmlobjects.gml.model.geometry.primitives.Polygon;
import org.xmlobjects.gml.model.geometry.primitives.SurfaceProperty;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class TextureBuilder {
    private final CityJSONBuilderHelper helper;

    TextureBuilder(CityJSONBuilderHelper helper) {
        this.helper = helper;
    }

    void build(JsonNode textures, List geometries, AppearanceBuilder appearanceBuilder, GeometryObject geometryObject) {
        TextureVerticesBuilder textureVerticesBuilder = appearanceBuilder.getTextureVerticesBuilder();
        Iterator> iterator = textures.fields();
        while (iterator.hasNext()) {
            Map.Entry entry = iterator.next();
            List flatValues = flatValues(entry.getValue().path(Fields.VALUES));

            for (int index = 0; index < flatValues.size() && index < geometries.size(); index++) {
                JsonNode surface = flatValues.get(index);
                Polygon polygon = (Polygon) geometries.get(index).getObject();

                ParameterizedTexture texture = null;
                TexCoordList texCoordList = new TexCoordList();

                for (int ringNo = 0; ringNo < surface.size(); ringNo++) {
                    JsonNode ring = surface.get(ringNo);
                    int textureIndex = ring.get(0).asInt(-1);
                    if (textureIndex == -1) {
                        break;
                    }

                    if (texture == null) {
                        texture = appearanceBuilder.getOrCreateTexture(
                                textureIndex, entry.getKey(),
                                geometryObject.getAppearanceInfo());
                        if (texture == null) {
                            break;
                        }
                    }

                    List textureVertices = textureVerticesBuilder.getTextureVertices((ArrayNode) ring);
                    if (!textureVertices.isEmpty()) {
                        RingReference target = getRingReference(polygon, ringNo);
                        if (target != null) {
                            texCoordList.getTextureCoordinates().add(new TextureCoordinates(textureVertices, target));
                        }
                    }
                }

                if (texture != null && texCoordList.isSetTextureCoordinates()) {
                    TextureAssociation textureAssociation = new TextureAssociation();
                    textureAssociation.setTarget(new GeometryReference("#" + helper.getOrCreateId(polygon)));
                    textureAssociation.setTextureParameterization(new AbstractTextureParameterizationProperty(texCoordList));
                    texture.getTextureParameterizations().add(new TextureAssociationProperty(textureAssociation));
                }
            }
        }
    }

    private RingReference getRingReference(Polygon polygon, int ringNo) {
        LinearRing linearRing = null;
        if (ringNo == 0) {
            linearRing = (LinearRing) polygon.getExterior().getObject();
        } else if (polygon.isSetInterior() && --ringNo < polygon.getInterior().size()) {
            linearRing = (LinearRing) polygon.getInterior().get(ringNo).getObject();
        }

        return linearRing != null ?
                new RingReference("#" + helper.getOrCreateId(linearRing)) :
                null;
    }

    private List flatValues(JsonNode values) {
        List flatValues = new ArrayList<>();
        flatValues(values, flatValues);
        return flatValues;
    }

    private void flatValues(JsonNode texture, List values) {
        if (texture.path(0).isArray()
                && texture.get(0).path(0).isArray()
                && texture.get(0).get(0).path(0).isValueNode()) {
            for (JsonNode element : texture) {
                values.add(element);
            }
        } else {
            for (JsonNode element : texture) {
                flatValues(element, values);
            }
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy