Hi, I am trying to improve my knowledge in class coding.
I have DEMs (Digital Elevation Map) which is a fancy word in engineering to say heightmap. I have made a ScriptableObject class to handle the data of these heightmaps.
[CreateAssetMenu(fileName = "New DEM", menuName = "Tools/Terrain Generator/DEM")]
public class DEM : ScriptableObject {
public static float scale = 0.5f;
[SerializeField] ComputeShader computeDEM;
[Header("Image")]
[SerializeField] Texture2D imageHeightMap;
[SerializeField] float meterPerPixel;
[SerializeField] float minValue;
[Header("Coordinates Range")]
[SerializeField] Vector2 latitudeRange;
[SerializeField] Vector2 longitudeRange;
}
The issue is I have 2 types of DEMs, Polar and Cylindrical and both need a different process in order to get the same data. Let me explain.
For instance, I need to convert my coordinates (latitude, longitude) to pixels coordinates. So I have created a method for this:
public Vector2 GetPixelCoordinates(Vector2 coordinates)
{
}
But I need to make 2 different processes depending on the type of the DEM. I could use a simple if statement but it is not very scalable and I want to use this case to improve my coding skills.
I also tried to create an abstract class which I guess is the best case. I have done it like this:
public abstract class DEMStructure
{
public static float scale = 0.5f;
public DEMData data { get; private set; }
public DEMStructure(DEMData data)
{
this.data = data;
}
public abstract Vector2 GetPixelCoordinates(Vector2 coordinates);
public abstract bool IsInsideSquare(Vector2 minBorderCoordinates, Vector2 maxBorderCoordinates);
}
public class CylindricalDEM : DEMStructure
{
public CylindricalDEM(DEMData data) : base(data) { }
public override Vector2 GetPixelCoordinates(Vector2 coordinates)
{
return Vector2.zero;
}
public override bool IsInsideSquare(Vector2 minBorderCoordinates, Vector2 maxBorderCoordinates)
{
return false;
}
}
public class PolarDEM : DEMStructure
{
public PolarDEM(DEMData data) : base(data) { }
public override Vector2 GetPixelCoordinates(Vector2 coordinates)
{
return Vector2.zero;
}
public override bool IsInsideSquare(Vector2 minBorderCoordinates, Vector2 maxBorderCoordinates)
{
return false;
}
}
I have removed all the computation to keep it very simple. The only thing you have to know is I need the data to perform the computation in the GetPixelCoordinates method.
Here is my problem, it means I need to always call the data variable and in the data class add a public getter for all the field.
My question is, is there a better way to make this?
To resume, I have a ScriptableObject which handle the data of the DEMs. There are 2 types of DEMs, Polar and Cylindrical. Some methods can be applied on both like getting two dimensional float array and some others need to be different like getting the pixel coordinates.