001/*
002 * Copyright (c) 2025 See AUTHORS file.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *   http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 */
017
018package com.github.tommyettinger.kryo.libgdx;
019
020import com.badlogic.gdx.math.Matrix4;
021import com.badlogic.gdx.math.Vector3;
022import com.badlogic.gdx.math.collision.BoundingBox;
023import com.badlogic.gdx.math.collision.OrientedBoundingBox;
024import com.esotericsoftware.kryo.Kryo;
025import com.esotericsoftware.kryo.Serializer;
026import com.esotericsoftware.kryo.io.Input;
027import com.esotericsoftware.kryo.io.Output;
028
029/**
030 * Kryo {@link Serializer} for libGDX {@link OrientedBoundingBox}s.
031 */
032public class OrientedBoundingBoxSerializer extends Serializer<OrientedBoundingBox> {
033
034    public OrientedBoundingBoxSerializer() {
035        setImmutable(false);
036    }
037
038    @Override
039    public void write(final Kryo kryo, final Output output, final OrientedBoundingBox data) {
040        final BoundingBox bb = data.getBounds();
041        output.writeFloat(bb.min.x);
042        output.writeFloat(bb.min.y);
043        output.writeFloat(bb.min.z);
044        output.writeFloat(bb.max.x);
045        output.writeFloat(bb.max.y);
046        output.writeFloat(bb.max.z);
047        float[] val = data.transform.val;
048        for (int i = 0; i < 16; i++) {
049            output.writeFloat(val[i]);
050        }
051    }
052
053    @Override
054    public OrientedBoundingBox read(final Kryo kryo, final Input input, final Class<? extends OrientedBoundingBox> dataClass) {
055        BoundingBox bb = new BoundingBox(
056                new Vector3(input.readFloat(), input.readFloat(), input.readFloat()),
057                new Vector3(input.readFloat(), input.readFloat(), input.readFloat()));
058        final Matrix4 tr = new Matrix4();
059        final float[] val = tr.val;
060        for (int i = 0; i < 16; i++) {
061            val[i] = input.readFloat();
062        }
063
064        return new OrientedBoundingBox(bb, tr);
065    }
066
067    @Override
068    public OrientedBoundingBox copy(Kryo kryo, OrientedBoundingBox original) {
069        return new OrientedBoundingBox(original.getBounds(), original.getTransform());
070    }
071}