Skip to content

Commit

Permalink
overwritten equals and hash function to not only compare memory address
Browse files Browse the repository at this point in the history
  • Loading branch information
rahelarnold98 committed Aug 28, 2024
1 parent 9d75452 commit 7e6b509
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,26 @@ class Material : JavaSerializable {
val EMPTY = Material()
}

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other !is Material) return false

return materialMeshes == other.materialMeshes &&
materialTexture == other.materialTexture &&
materialDiffuseColor == other.materialDiffuseColor
}

override fun hashCode(): Int {
var result = materialMeshes.hashCode()
result = 31 * result + (materialTexture?.hashCode() ?: 0)
result = 31 * result + materialDiffuseColor.hashCode()
return result
}

override fun toString(): String {
return "Material(materialMeshes=$materialMeshes, materialTexture=$materialTexture, materialDiffuseColor=$materialDiffuseColor)"
}

/**
* @return A MinimalBoundingBox which encloses all MinimalBoundingBoxes from containing meshes.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,26 @@ class Mesh(
private val LOGGER: Logger = LogManager.getLogger()
}

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other !is Mesh) return false

if (!positions.contentEquals(other.positions)) return false
if (!normals.contentEquals(other.normals)) return false
if (!textureCoords.contentEquals(other.textureCoords)) return false
if (!idx.contentEquals(other.idx)) return false

return true
}

override fun hashCode(): Int {
var result = positions.contentHashCode()
result = 31 * result + (normals?.contentHashCode() ?: 0)
result = 31 * result + textureCoords.contentHashCode()
result = 31 * result + idx.contentHashCode()
return result
}

/**
* Number of all vertices in the mesh.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,40 @@ data class Texture(
}
}

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other !is Texture) return false

if (texturePath != other.texturePath) return false
if (!compareBufferedImages(textureImage, other.textureImage)) return false

return true
}

override fun hashCode(): Int {
var result = texturePath?.hashCode() ?: 0
result = 31 * result + (textureImage?.let { it.hashCode() } ?: 0)
return result
}

/**
* Compares two BufferedImages pixel by pixel.
*/
private fun compareBufferedImages(img1: BufferedImage?, img2: BufferedImage?): Boolean {
if (img1 == null || img2 == null) return img1 == img2
if (img1.width != img2.width || img1.height != img2.height) return false

for (y in 0 until img1.height) {
for (x in 0 until img1.width) {
if (img1.getRGB(x, y) != img2.getRGB(x, y)) {
return false
}
}
}

return true
}

constructor(texturePath: String) : this(texturePath, null)

constructor(textureImage: BufferedImage) : this(null, textureImage)
Expand Down

0 comments on commit 7e6b509

Please sign in to comment.