< Summary

Class:FoodItem
Assembly:Test
File(s):E:/Unity/Unity Project/VR-Cooking-Demo/Assets/_Scripts/FoodItem.cs
Covered lines:99
Uncovered lines:0
Coverable lines:99
Total lines:165
Line coverage:100% (99 of 99)
Covered branches:0
Total branches:0
Covered methods:8
Total methods:8
Method coverage:100% (8 of 8)

Coverage History

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
FoodItem()0%000100%
Start()0%00098.21%
MeshVolume()0%000100%
SignedVolumeOfTriangle(...)0%000100%
VolumeOfMesh(...)0%000100%
StartCountdowm(...)0%000100%
EndCountDown()0%000100%
LockInPotCountdown()0%000100%

File(s)

E:/Unity/Unity Project/VR-Cooking-Demo/Assets/_Scripts/FoodItem.cs

#LineLine coverage
 1/*
 2 * Code adapted from:
 3 *      https://answers.unity.com/questions/52664/how-would-one-calculate-a-3d-mesh-volume-in-unity.html
 4 * by:
 5 *      Dimitrios Vlachos
 6 *      djv1@student.london.ac.uk
 7 *      dimitri.j.vlachos@gmail.com
 8 */
 9
 10using System.Collections;
 11using System.Collections.Generic;
 12using UnityEngine;
 13
 14public class FoodItem : MonoBehaviour
 15{
 16    public enum FoodType { Carrot, Cheese, Potato, Other };
 17    public FoodType type;
 18
 19    [Header("Nutritional information")]
 20    public float weight;
 21    public float density;
 22    public float calories;
 23    public float protein;
 24    public float carbohydrates;
 25    public float sugar;
 26    public float fibre;
 27    public float fat;
 28    public float modifier;
 29
 30    [Header("Properties")]
 1231    public float lockTimer = 3;
 32
 1233    public bool hasBeenCut = false;
 1234    public bool inPot = false;
 35
 36
 37    // Start is called before the first frame update
 38    void Start()
 639    {
 40        float volume;
 41
 642        switch (tag)
 43        { // Nutrition per 100g
 44            case "Carrot":
 145                modifier = 2600f;
 146                volume = MeshVolume();
 147                type= FoodType.Carrot;
 148                density = 1.04f; // g/cm3     d = g/v  g = d*v
 149                calories = 41f;
 150                protein = 0.9f;
 151                carbohydrates = 9.6f;
 152                sugar = 4.7f;
 153                fibre = 2.8f;
 154                fat = 0.2f;
 155                break;
 56            case "Cheese":
 157                modifier = 232f;
 158                volume = MeshVolume();
 159                type = FoodType.Cheese;
 160                density = 1.075f; // g/cm3
 161                calories = 404f;
 162                protein = 23f;
 163                carbohydrates = 3.1f;
 164                sugar = 0.5f;
 165                fibre = 0f;
 166                fat = 33f;
 167                break;
 68            case "Potato":
 369                modifier = 1300f;
 370                volume = MeshVolume();
 371                type = FoodType.Potato;
 372                density = 1.09f; // g/cm3
 373                calories = 93f;
 374                protein = 2.5f;
 375                carbohydrates = 21f;
 376                sugar = 1.2f;
 377                fibre = 2.2f;
 378                fat = 0.1f;
 379                break;
 80            default:
 81                //Debug.Log("In the default");
 182                modifier = 0f;
 183                volume = 0f;
 184                density = 0f;
 185                calories = 0f;
 186                protein = 0f;
 187                carbohydrates = 0f;
 188                sugar = 0f;
 189                fibre = 0f;
 190                fat = 0f;
 191                break;
 92        }
 93
 94        // Guard statement to prevent unecessary calculations
 695        if (tag.Equals("Other")) return;
 96
 697        weight = density * volume;
 698        float proportion = weight / 100f; // Per 100g
 699        calories *= proportion;
 6100        protein *= proportion;
 6101        carbohydrates *= proportion;
 6102        sugar *= proportion;
 6103        fibre *= proportion;
 6104        fat *= proportion;
 6105    }
 106
 107    private float MeshVolume()
 5108    {
 5109        MeshFilter meshFilter = GetComponentInChildren<MeshFilter>();
 5110        Mesh mesh = meshFilter.sharedMesh;
 5111        float volume = VolumeOfMesh(mesh);// * modifier; // Convert to cm3
 5112        volume *= modifier;
 5113        string msg = "The volume of the mesh is " + volume + " cm3.";
 114        //Debug.Log(msg);
 5115        return volume;
 5116    }
 117
 118    private float SignedVolumeOfTriangle(Vector3 p1, Vector3 p2, Vector3 p3)
 8008119    {
 8008120        float v321 = p3.x * p2.y * p1.z;
 8008121        float v231 = p2.x * p3.y * p1.z;
 8008122        float v312 = p3.x * p1.y * p2.z;
 8008123        float v132 = p1.x * p3.y * p2.z;
 8008124        float v213 = p2.x * p1.y * p3.z;
 8008125        float v123 = p1.x * p2.y * p3.z;
 126
 8008127        return (1.0f / 6.0f) * (-v321 + v231 + v312 - v132 - v213 + v123);
 8008128    }
 129
 130    private float VolumeOfMesh(Mesh mesh)
 5131    {
 5132        float volume = 0;
 133
 5134        Vector3[] vertices = mesh.vertices;
 5135        int[] triangles = mesh.triangles;
 136
 16026137        for (int i = 0; i < triangles.Length; i += 3)
 8008138        {
 8008139            Vector3 p1 = vertices[triangles[i + 0]];
 8008140            Vector3 p2 = vertices[triangles[i + 1]];
 8008141            Vector3 p3 = vertices[triangles[i + 2]];
 8008142            volume += SignedVolumeOfTriangle(p1, p2, p3);
 8008143        }
 5144        return Mathf.Abs(volume);
 5145    }
 146
 147    private Coroutine c;
 148
 149    public void StartCountdowm(GameObject p)
 4150    {
 4151        Debug.Log("Countdown begun");
 4152        c = StartCoroutine(LockInPotCountdown(p));
 4153    }
 154
 155    public void EndCountDown()
 5156    {
 5157        StopCoroutine(c);
 5158    }
 159
 160    private IEnumerator LockInPotCountdown(GameObject p)
 4161    {
 4162        yield return new WaitForSeconds(lockTimer);
 2163        transform.parent = p.transform;
 2164    }
 165}